Weighted matrix to edge list

1 回表示 (過去 30 日間)
Marie
Marie 2017 年 11 月 17 日
コメント済み: Marie 2017 年 11 月 17 日
Hello,
Is there a quick way to transform a matrix such as
[10 1 9; 1 5 4; 2 1 5]
into an edge list with the information containing both i and j (also when i=j) and the value of the pair. For example
[1 1 10; 1 2 1; 1 3 9; 2 1 1; 2 2 5; 2 3 4; 3 1 2; 3 2 1; 3 3 5].
Thanks in advance for any help.

採用された回答

Stephen23
Stephen23 2017 年 11 月 17 日
編集済み: Stephen23 2017 年 11 月 17 日
One way based around ndgrid:
>> M = [10 1 9; 1 5 4; 2 1 5];
>> N = M.';
>> S = size(N);
>> [R,C] = ndgrid(1:S(1),1:S(2));
>> [C(:),R(:),N(:)]
ans =
1 1 10
1 2 1
1 3 9
2 1 1
2 2 5
2 3 4
3 1 2
3 2 1
3 3 5
If the order is not important then you do not need the transpose. Note that in MATLAB the first dimension is down the columns.
  1 件のコメント
Marie
Marie 2017 年 11 月 17 日
Thanks a lot! This is very helpful.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by