[英]assign a cell to a cell array
I have the following matrix : 我有以下矩阵:
adj = O1 O2 O3 O4 S1 S2 S3 S4 S5
O1 0 0 0 0 0 1 1 0 0
O2 0 0 0 0 0 1 0 1 0
O3 0 0 0 0 0 1 1 0 0
O4 0 0 0 0 0 0 0 1 1
S1 0 0 1 0 0 0 0 0 0
S2 0 1 0 0 0 0 0 0 0
S3 0 1 1 0 0 0 0 0 0
S4 0 1 0 1 0 0 0 0 0
S5 0 0 0 1 0 0 0 0 0
I have a cellarray nodeNames that contains the labels of the lines and columns of my matrix so : 我有一个cellarray nodeNames,其中包含矩阵行和列的标签,因此:
nodeNames = {O1 O2 O3 O4 S1 S2 S3 S4 S5}
I want to do the following: browse the matirce adj in the lines from the nodes S
(i = 5: 9) and for the columns the nodes O
(j = 1; 4) if adj (i, j) = 1
then get the label of the node Oj
from nodeNames
and put it in the cell w {i}
of a new cellarray that we will name w
. 我要执行以下操作:在节点
S
(i = 5:9)的行中浏览matirce adj,对于列O
(j = 1; 4)的列,如果adj (i, j) = 1
则得到从nodeNames
删除节点Oj
的标签,并将其放入新单元格的单元格w {i}
中,我们将其命名为w
。 so I want to have the following: 所以我想要以下内容:
w{1}={O3}.
w{2}={O2}.
w{3}={O2 O3}.
w{4}={O2 O4}.
w{5}={O4}.
I have tried to do that but I get an empty cellarray in my result : 我已经尝试过这样做,但是结果是空单元格:
for i=5 : 9
k=1;
for j=1:4
if adj(i,j)==1;
w{i}{k}=nodeNames{j};
end
k=k+1;
end
end
I know that the problem is in the assignment to the cellarray w
but can't get the right one , Any suggestion ? 我知道问题出在分配给细胞阵列
w
的问题上,但找不到正确的方法,有什么建议吗?
nodeNames
or nodNames
, also your k=k+1 must be in the if-statement. nodeNames
或nodNames
,您的k = k + 1也必须在if语句中。
adj=[0,0,0,0,0,1,1,0,0;0,0,0,0,0,1,0,1,0;0,0,0,0,0,1,1,0,0;0,0,0,0,0,0,0,1,1;0,0,1,0,0,0,0,0,0;0,1,0,0,0,0,0,0,0;0,1,1,0,0,0,0,0,0;0,1,0,1,0,0,0,0,0;0,0,0,1,0,0,0,0,0]==1;
nodeNames = {'O1','O2','O3','O4','S1','S2','S3','S4','S5'};
w={};
for i = 5:9
k=1;
for j=1:4
if adj(i,j)
w{i-4,k}=nodeNames{j};
k=k+1;
end
end
end
Result: 结果:
w =
5×2 cell array
'O3' []
'O2' []
'O2' 'O3'
'O2' 'O4'
'O4' []
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.