listing/enumerating numbers

1 回表示 (過去 30 日間)
cgo
cgo 2015 年 11 月 10 日
編集済み: Thorsten 2015 年 11 月 10 日
I have the following matrix, named interact:
interact = [1 2;
1 3;
2 1;
2 3;
3 1;
3 4;
4 1]
This matrix means that 1 and 2 interact, 1 and 3 interact, and so on. (just a matrix full of indices.)
Now I want to perform this operation/task.
All that 1 interacts will be added and stored in a matrix. Meaning the interaction between 1 and 2 + interaction between 1 and 3 = interaction of 1.
Similarly, separate matrices for interaction of 2, 3, 4, etc.
How do I do this in matlab?

採用された回答

Thorsten
Thorsten 2015 年 11 月 10 日
編集済み: Thorsten 2015 年 11 月 10 日
for val=1:4
iv{val} = interact(ismember(interact(:,1), val), 2);
end

その他の回答 (1 件)

Guillaume
Guillaume 2015 年 11 月 10 日
Simply use accumarray:
interact = [1 2; 1 3; 2 1; 2 3; 3 1; 3 4; 4 1];
interactwith = accumarray(interact(:, 1), interact(:, 2), [], @(v) {v})
accumarray uses the subs argument (here your first column) to group together the val argument (here your 2nd column) into a vector which at the end it passes to the fun function argument. Here that function is an anonymous function that just returns the vector as a cell array.

カテゴリ

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