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!