如何在matlab中进行遗传运算
我有两个矩阵。一个是如何在matlab中进行遗传运算,matlab,matrix,Matlab,Matrix,我有两个矩阵。一个是I单位矩阵,另一个是A,对角线为0,在其他位置混合了1和0。保持恒等矩阵不变,从左到右扫描行 I是单位矩阵 I = 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0
I
单位矩阵,另一个是A
,对角线为0,在其他位置混合了1和0。保持恒等矩阵不变,从左到右扫描行
I
是单位矩阵
I =
1 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0
0 0 1 0 0 0 0 0
0 0 0 1 0 0 0 0
0 0 0 0 1 0 0 0
0 0 0 0 0 1 0 0
0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 1
A
矩阵:
A =
0 1 1 1 0 0 0 0
1 0 1 1 0 0 0 0
1 1 0 1 1 1 0 0
1 1 1 0 1 1 1 0
0 1 1 1 0 1 1 1
0 0 1 1 1 0 1 1
0 0 0 1 1 1 0 1
0 0 0 0 1 1 1 0
结果
矩阵
Result =
1 0 0 0 1 0 0 0
0 1 0 0 0 1 0 0
0 0 1 0 0 0 1 0
0 0 0 1 0 0 0 1
试试这个:
~(I|A)
其中~
为非逻辑,而|
为逻辑或。请尝试以下操作:
~(I|A)
其中,
~
为非逻辑,而|
为逻辑或。更新最终更新:调整为仅使用一个1的矩阵
因为我发现很难理解这个问题,这里所述的过程是对下面代码的解释。我使用第3行(第三行)作为示例,只要具体示例可行 (1) 查找节点3未连接到的所有节点,因为它们可以同时传输。为此,请执行以下操作:在
I
和A
的第3行之间取NOR
(false和false=>true),然后将I
(第3列)中的1添加到该行中。这是TempResult
I(3,:) : 0 0 1 0 0 0 0 0
A(3,:) : 1 1 0 1 1 1 0 0
TempResult: 0 0 1 0 0 0 1 1
(3) 在获得的行和当前的结果
之间逐行应用NAND(因为到目前为止它已经建立)。(获得的向量与下一行进行与非门运算,连续通过结果的所有行
)
(4) 在通过上述NAND ing获得的最后一行和步骤(2)的res
之间取AND
(5) 检查前两个之后的每个1
:查找其索引和1
,并使用A(索引,索引)
后处理获得的矩阵:删除重复的行,并应用最终的“过滤”(参见代码)
其中一些是逐字记录的,如澄清中所述
规范注释中说明了该程序
function Result=nor\u mat(A)
%取一个方阵a
I=眼睛(尺寸(A,1));%A的大小的单位矩阵
结果=[];
i=1时:尺寸(A)
%也不是关于“i”和“A”的tempRes i
tmp1=~I(I,:)&A(I,:);
%查找1位于“I”中的列(索引)
一个ind=find(I(I,:)=1,1);
%将“I”中的1放在TempResult的相应列中
tmp1(一个ind)=1;
%对于大于1的行:结果中TempRes1和上一行之间的NAND
tempRes=tmp1;
如果(i>1)
tmp2=tmp1;
对于j=1:尺寸(结果,1)
行_tmp1=tmp2&结果(j,:);
row_tmp=~ row_tmp1;
tmp2=行\u tmp;
结束
%现在在最终“tmp2”结果和tmp1之间执行和
tempRes=tmp1和tmp2;
结束
%查找(前两个)非零列,首先获取不是来自I的
col_two=查找(tempRes,2);
col=col_two(find(col_two~=one_ind,1));
%对于1(来自I)的矩阵,上面不会找到任何东西
如果是空的(col)
col=i;
结束
%检查冲突:请不要在列表的第i行和第col行之间使用NOR
res=~A(i,:)&~A(col,:);
%将I的1放在结果行的相应列中
res(one_ind)=1;
%最终清理:在前两次清理后检查所有1
%在这样的元素上执行和,其中A的条目位于该位置(索引,索引)
额外的=find(res==1);
如果长度(额外长度)>2
对于k=1:长度(额外的);
ind=额外的(k);
res(ind)=~res(ind)&~A(ind,ind);
结束
结束
%将此结果行添加到“结果”矩阵
结果=[Result;res];
结束
%其他调整。如果需要,取消注释。
%%对于结果每行中索引为1的所有结果行之间的NOR
%结果_new=[];
%对于i=1:大小(结果,1)
%cols_one=find(结果(i,:)=1);
%res_new=结果(cols_one(1),:);
%对于j=2:长度(cols_one)
%res_tmp=res_new;
%res_new=~Result(cols_one(j),:)&~res_tmp;
%结束
%Result_new=[Result_new;res_new];
%结束
结果=唯一(结果“行”,“稳定”);
%最终“过滤”
ind=累计和(结果);%累计和(按列)
结果(ind>1)=0;
结果(和(结果')==0,:)=[];
%打印出来
对于i=1:大小(结果,1)
fprintf('%2d',结果(i,:);fprintf('\n');
结束
fprintf('\n');
A和B之间的NOR操作(false和false==>true)在上面的代码中实现为[pseudo code]:(不是A)和(不是B)
。在Matlab中,这可以写为:~A和~B
(对于向量)。据我所知,每一行代表当时可能传输的节点。这将打印
1 0 0 0 1 0 0 0
0 1 0 0 0 1 0 0
0 0 1 0 0 0 1 0
0 0 0 1 0 0 0 1
打印输出
--- row 1
I(1,:) : 1 0 0 0 0 0 0 0
A(1,:) : 0 1 1 1 0 0 0 0
tmp1: 1 0 0 0 1 1 1 1
Do NAND between tmp1 and previous rows of Result
Choose col 5 ("1" from I is in col 1)
Check columns 1 and 5 for conflict.
col 1: 0 1 1 1 0 0 0 0
col 5: 0 1 1 1 0 1 1 1
res : 1 0 0 0 1 0 0 0
Indices of all 1: 1 5
(Check all 1 after the first two, against A at that index.)
--- row 2
I(2,:) : 0 1 0 0 0 0 0 0
A(2,:) : 1 0 1 1 0 0 0 0
tmp1: 0 1 0 0 1 1 1 1
Do NAND between tmp1 and previous rows of Result
Do AND between result of the above and tmp1
tmp1: 0 1 0 0 1 1 1 1
Choose col 6 ("1" from I is in col 2)
Check columns 2 and 6 for conflict.
col 2: 1 0 1 1 0 0 0 0
col 6: 0 0 1 1 1 0 1 1
res : 0 1 0 0 0 1 0 0
Indices of all 1: 2 6
(Check all 1 after the first two, against A at that index.)
--- row 3
I(3,:) : 0 0 1 0 0 0 0 0
A(3,:) : 1 1 0 1 1 1 0 0
tmp1: 0 0 1 0 0 0 1 1
Do NAND between tmp1 and previous rows of Result
Do AND between result of the above and tmp1
tmp1: 0 0 1 0 0 0 1 1
Choose col 7 ("1" from I is in col 3)
Check columns 3 and 7 for conflict.
col 3: 1 1 0 1 1 1 0 0
col 7: 0 0 0 1 1 1 0 1
res : 0 0 1 0 0 0 1 0
Indices of all 1: 3 7
(Check all 1 after the first two, against A at that index.)
--- row 4
I(4,:) : 0 0 0 1 0 0 0 0
A(4,:) : 1 1 1 0 1 1 1 0
tmp1: 0 0 0 1 0 0 0 1
Do NAND between tmp1 and previous rows of Result
Do AND between result of the above and tmp1
tmp1: 0 0 0 1 0 0 0 1
Choose col 8 ("1" from I is in col 4)
Check columns 4 and 8 for conflict.
col 4: 1 1 1 0 1 1 1 0
col 8: 0 0 0 0 1 1 1 0
res : 0 0 0 1 0 0 0 1
Indices of all 1: 4 8
(Check all 1 after the first two, against A at that index.)
--- row 5
I(5,:) : 0 0 0 0 1 0 0 0
A(5,:) : 0 1 1 1 0 1 1 1
tmp1: 1 0 0 0 1 0 0 0
Do NAND between tmp1 and previous rows of Result
Do AND between result of the above and tmp1
tmp1: 1 0 0 0 1 0 0 0
Choose col 1 ("1" from I is in col 5)
Check columns 5 and 1 for conflict.
col 5: 0 1 1 1 0 1 1 1
col 1: 0 1 1 1 0 0 0 0
res : 1 0 0 0 1 0 0 0
Indices of all 1: 1 5
(Check all 1 after the first two, against A at that index.)
--- row 6
I(6,:) : 0 0 0 0 0 1 0 0
A(6,:) : 0 0 1 1 1 0 1 1
tmp1: 1 1 0 0 0 1 0 0
Do NAND between tmp1 and previous rows of Result
Do AND between result of the above and tmp1
tmp1: 1 1 0 0 0 1 0 0
Choose col 2 ("1" from I is in col 6)
Check columns 6 and 2 for conflict.
col 6: 0 0 1 1 1 0 1 1
col 2: 1 0 1 1 0 0 0 0
res : 0 1 0 0 0 1 0 0
Indices of all 1: 2 6
(Check all 1 after the first two, against A at that index.)
--- row 7
I(7,:) : 0 0 0 0 0 0 1 0
A(7,:) : 0 0 0 1 1 1 0 1
tmp1: 1 1 1 0 0 0 1 0
Do NAND between tmp1 and previous rows of Result
Do AND between result of the above and tmp1
tmp1: 1 1 1 0 0 0 1 0
Choose col 1 ("1" from I is in col 7)
Check columns 7 and 1 for conflict.
col 7: 0 0 0 1 1 1 0 1
col 1: 0 1 1 1 0 0 0 0
res : 1 0 0 0 0 0 1 0
Indices of all 1: 1 7
(Check all 1 after the first two, against A at that index.)
--- row 8
I(8,:) : 0 0 0 0 0 0 0 1
A(8,:) : 0 0 0 0 1 1 1 0
tmp1: 1 1 1 1 0 0 0 1
Do NAND between tmp1 and previous rows of Result
Do AND between result of the above and tmp1
tmp1: 1 1 1 1 0 0 0 1
Choose col 2 ("1" from I is in col 8)
Check columns 8 and 2 for conflict.
col 8: 0 0 0 0 1 1 1 0
col 2: 1 0 1 1 0 0 0 0
res : 0 1 0 0 0 0 0 1
Indices of all 1: 2 8
(Check all 1 after the first two, against A at that index.)
Prune duplicate rows, filter (see code). Result:
1 0 0 0 1 0 0 0
0 1 0 0 0 1 0 0
0 0 1 0 0 0 1 0
0 0 0 1 0 0 0 1
---第1行
我(1,:):100
A(1,:):01100
tmp1:100 01
在tmp1和结果的前几行之间执行NAND
选择第5列(“第1列中的1”)
检查第1列和第5列是否存在冲突。
列1:01100
第5列:01101
res:100100100
所有指数均为1:15
(对照该索引处的A,检查前两个之后的所有1。)
---第2排
我(2,:):01100
A(2,:):10100
tmp1:01101
在tmp1和结果的前几行之间执行NAND
在上述结果和tmp1之间执行和
tmp1:01101
选择第6列(“1”来自第2列中的I)
检查第2列和第6列是否存在冲突。
第2列:10100
第6列:0 0 1 1 0 1 1
res:0110010
所有指数均为1:26
(对照该索引处的A,检查前两个之后的所有1。)
---第3排
我(3,:):0 1 0 0 0 0
A(3,:):110
tmp1:001
在tmp1和结果的前几行之间执行NAND
在上述结果和tmp1之间执行和
tmp1:001
选择第7列(第3列中的“I”中的“1”)
检查第3列和第7列是否存在冲突。
第3列:110 110
第7列:0 0 1 1 0 1
res:0 1 0 0 1 0
全部指数1:3 7
(对照该索引处的A,检查前两个之后的所有1。