Array processing problem

1 回表示 (過去 30 日間)
Lulu
Lulu 2011 年 10 月 31 日
Hi,
I have the following problem. Let's say there is an 3x3 array 'arr': 0 0 0 0 0 0 0 0 0
There are 3 edges 'E': 1 2 1 3 2 3
Finally, there are 3 logical values 'val' that correspond to edges 'E': 1 0 1
Now I need to process 'arr' in such a way that all edges that have '1' are defined in 'arr', e.g. if E(1,:) = 1 2 and val(1) = 1, then the cell [1,2] in 'arr' has a value '1'. The output should be: arr= 0 1 1 0 0 1 0 0 0
I wrote the following code, but the last line does not work properly. Any help is highly appreciated. Thanks. array = [0 0 0; 0 0 0; 0 0 0]; E = [1 2; 1 3; 2 3]; val = [1; 0; 1]; len = length(E(:,1)); array(E(1:len,1),E(1:len,2)) = val(1:len,1);

採用された回答

Andrei Bobrov
Andrei Bobrov 2011 年 10 月 31 日
arr = zeros(3)
val = [1; 0; 1]
E = [1 2; 1 3; 2 3]
arr(sub2ind(size(arr),E(:,1),E(:,2))) = val
or
arr = accumarray(E,val,[3 3])
  1 件のコメント
Lulu
Lulu 2011 年 10 月 31 日
Thank you.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGraphics Object Programming についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by