Array processing problem
1 回表示 (過去 30 日間)
古いコメントを表示
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);
0 件のコメント
採用された回答
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])
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Graphics Object Programming についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!