如何从 ND 数组中提取非零元素的索引?
How to extract indices of non-zero elements from ND array?
我需要用 4 个不同的标签来标记有向图的边,所以我将它存储在一个 N*N*4 数组中。 (让我们坚持使用 4,这是我实际的第三维。)N 是图中的节点数。即对于条目 M(i,j,1),我会用第一种标签标记从节点 i 到节点 j 的边。这是一个可以玩的玩具示例。
M(2,1,1)=1
表示我在节点2到节点1的边上添加标签文本'first label'。M(3,2,2)=1
表示我在节点的边上添加标签文本'second label' 3到节点2。3D数组中的每个'page'是一种标签。
M(:,:,1)= 0 0 0 M(:,:,2)= 0 0 1
1 0 1 0 0 0
0 0 0 0 1 0
但是我不知道如何将N*N*4数组M
转换成s
和labeledge(h,s,t,Labels)
中的t
。 s
是源节点的索引,而 t
是目标节点的索引。
理想情况下,情节是这样的:
s = [2 2 1 3];
t = [1 3 3 2];
G = digraph(s,t);
figure;
h = plot(G);
关键问题是如何从 M 中获取 s1、t1、s2 和 t2,如下所示:
labeledge(h,[2 2], [1 3], 'first label');
labeledge(h,[1 3], [3 2], 'second label');
仅供参考:M(:,:,1)
和 M(:,:,2)
没有相同的非零条目。
一些背景知识:
在你的问题中,s
和 t
的顺序是相互关联的,即如果 t
的元素也被打乱,s
的打乱并不重要以相同的顺序。例如;如果
s = [2 2 1 3];
t = [1 3 3 2];
%The combination of s and t given by you
以上将给出与以下相同的结果:
s = [2 3 2 1];
t = [1 2 3 3];
% replaced column 2 with column 3, column 3 with column 2, and column 4 with column 3
这样的组合还有很多。
我将要展示的代码将为给定的 M
:
提供以下组合
s = [2 2 3 1]; %named as snew in my code
t = [1 3 2 3]; %named as tnew in my code
%In this combination, column 3 and 4 are inter-changed (See the combination given by you)
代码:
for k=1:size(M,3) %Looping depending on the third dimension
[r,c]= find(M(:,:,k)); %Finding non-zero elements
%Storing the values in a cell since the number of non-zero elements in each
%slice of M can be different
s{k}=r; t{k}=c;
end
以上代码给出s{1}=[2; 2]
、s{2}=[3; 1]
、t{1}=[1; 3]
和t{2}=[2; 3]
.
现在您可以通过使用以下代码从上述代码中提取 s
和 t
来找到您问题中给出的组合:
snew=vertcat(s{:}); %Giving a different name to avoid confusion
tnew=vertcat(t{:});
现在您可以使用 snew
和 tnew
制作 digraph
并使用 s{1}
、s{2}
、t{1}
和 [= 标记边缘33=].
要标记边缘,您可以使用以下循环:
for k=1:length(s)
labeledge(h,s{k}, t{k}, ['label ',num2str(k)]);
end
如果要制作的标签太多,这将解决标签问题。将标签设为 first
, second
,.... 会很麻烦,而且可能没有必要。
输出:
通过上述修改,您将得到以下输出:
我需要用 4 个不同的标签来标记有向图的边,所以我将它存储在一个 N*N*4 数组中。 (让我们坚持使用 4,这是我实际的第三维。)N 是图中的节点数。即对于条目 M(i,j,1),我会用第一种标签标记从节点 i 到节点 j 的边。这是一个可以玩的玩具示例。
M(2,1,1)=1
表示我在节点2到节点1的边上添加标签文本'first label'。M(3,2,2)=1
表示我在节点的边上添加标签文本'second label' 3到节点2。3D数组中的每个'page'是一种标签。
M(:,:,1)= 0 0 0 M(:,:,2)= 0 0 1
1 0 1 0 0 0
0 0 0 0 1 0
但是我不知道如何将N*N*4数组M
转换成s
和labeledge(h,s,t,Labels)
中的t
。 s
是源节点的索引,而 t
是目标节点的索引。
理想情况下,情节是这样的:
s = [2 2 1 3];
t = [1 3 3 2];
G = digraph(s,t);
figure;
h = plot(G);
关键问题是如何从 M 中获取 s1、t1、s2 和 t2,如下所示:
labeledge(h,[2 2], [1 3], 'first label');
labeledge(h,[1 3], [3 2], 'second label');
M(:,:,1)
和 M(:,:,2)
没有相同的非零条目。
一些背景知识:
在你的问题中,s
和 t
的顺序是相互关联的,即如果 t
的元素也被打乱,s
的打乱并不重要以相同的顺序。例如;如果
s = [2 2 1 3];
t = [1 3 3 2];
%The combination of s and t given by you
以上将给出与以下相同的结果:
s = [2 3 2 1];
t = [1 2 3 3];
% replaced column 2 with column 3, column 3 with column 2, and column 4 with column 3
这样的组合还有很多。
我将要展示的代码将为给定的 M
:
s = [2 2 3 1]; %named as snew in my code
t = [1 3 2 3]; %named as tnew in my code
%In this combination, column 3 and 4 are inter-changed (See the combination given by you)
代码:
for k=1:size(M,3) %Looping depending on the third dimension
[r,c]= find(M(:,:,k)); %Finding non-zero elements
%Storing the values in a cell since the number of non-zero elements in each
%slice of M can be different
s{k}=r; t{k}=c;
end
以上代码给出s{1}=[2; 2]
、s{2}=[3; 1]
、t{1}=[1; 3]
和t{2}=[2; 3]
.
现在您可以通过使用以下代码从上述代码中提取 s
和 t
来找到您问题中给出的组合:
snew=vertcat(s{:}); %Giving a different name to avoid confusion
tnew=vertcat(t{:});
现在您可以使用 snew
和 tnew
制作 digraph
并使用 s{1}
、s{2}
、t{1}
和 [= 标记边缘33=].
要标记边缘,您可以使用以下循环:
for k=1:length(s)
labeledge(h,s{k}, t{k}, ['label ',num2str(k)]);
end
如果要制作的标签太多,这将解决标签问题。将标签设为 first
, second
,.... 会很麻烦,而且可能没有必要。
输出:
通过上述修改,您将得到以下输出: