简体   繁体   中英

Convert adjacency matrix to specific edge list in MATLAB

If I have the matrix

  1 0 0  
  0 0 1 
  0 0 0 

and I want this form in MATLAB

1 2 3  1 2 3  1 2 3
1 1 1  2 2 2  3 3 3
1 0 0  0 0 0  0 1 0

also I want the values of third row in result. ie ans= [1 0 0 0 0 0 0 1 0]

Here you go -

[X,Y] = ndgrid(1:size(A,1),1:size(A,2));
out = [X(:).' ; Y(:).' ; A(:).']

For the last part of your question, use the last row of out : out(end,:) or A(:).' .

Sample run -

>> A
A =
     1     0     0
     0     0     1
     0     0     0
>> [X,Y] = ndgrid(1:size(A,1),1:size(A,2));
>> out = [X(:).' ; Y(:).' ; A(:).']
out =
     1     2     3     1     2     3     1     2     3
     1     1     1     2     2     2     3     3     3
     1     0     0     0     0     0     0     1     0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM