construction of matrix using specified data sets

1 回表示 (過去 30 日間)
John
John 2013 年 6 月 15 日
I've the following dataset:
node1 =
1
2
3
4
1
node2 =
2
3
4
5
7
cost =
23 for arc(1,2) # arc is a combination of node1 and node2
14 for arc(2,3)
123 for arc(3,4)
111 for arc(4,5)
60 for arc(1,7)
I would like to create a nxm matrix say C with rows and columns being the maximum of (nodes1,nodes2)(in this case it would be 7 so it would be a 7x7 matrix) such that C will have the values of the column vector 'cost' for its respective arcs ( arc is connection between elements of node1 and node2) otherwise it would have a value 0. So my resulting matrix should look like this:
[0 23 0 0 0 0 60;
23 0 14 0 0 0 0;
0 14 0 123 0 0 0;
0 0 123 0 111 0;
0 0 0 111 0 0 0;
0 0 0 0 0 0 0;
60 0 0 0 0 0 0]
I am not sure I can do this using matlab. Please help.
  2 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 6 月 15 日
How did you get ?
23 for arc(1,2)
John
John 2013 年 6 月 15 日
編集済み: John 2013 年 6 月 15 日
it is already given to me in my data set.

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

採用された回答

Walter Roberson
Walter Roberson 2013 年 6 月 15 日
data = [1 2 23; 2 3 14; 3 4 123; 4 5 111; 1 7 60];
T = accumarray(data(:,1:2), data(:,3), max(max(data(:,1:2))) );
T = T + T.';
The above handles the symmetry, except that if there are any arcs from a node to itself the cost value would be twice as high as it should be.
  1 件のコメント
John
John 2013 年 6 月 16 日
Thank you!

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

その他の回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 6 月 15 日
out=zeros(7)
out(1,2)=23
out(2,3)=14
out(3,4)=123
out(4,5)=111
out(1,7)=60

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by