Array processing problem

1 vue (au cours des 30 derniers jours)
Lulu
Lulu le 31 Oct 2011
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);

Réponse acceptée

Andrei Bobrov
Andrei Bobrov le 31 Oct 2011
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 commentaire
Lulu
Lulu le 31 Oct 2011
Thank you.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Graphics Object Programming dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by