Array processing problem

8 次查看(过去 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])

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Operators and Elementary Operations 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by