Using find function output locations to sum over values from another matrix.
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello,
I have a question concerning find, or many someone could propose another way to do what I am trying to do.
I have three arrays, all the same size. What I want to do is essentially find the locations of the cells in the first array that are equal to 1, or some other specified value, then use those locations to sum over the values of the same locations in the second and third arrays if that makes sense.
Right now I am working with something like this:
M(n,n,3) %is the array storing each layer of the array.
[i j] = M(:,:,1) == 1;
s1 = sum(sum(M(i,j,2)))
s2 = sum(sum(M(i,j,2)))
This, however, does not give the correct output on double checking them.
I also thought that maybe I should not be using the find function, but instead creating a new matrix like so:
A = (M(:,:,1) == 1)
However I still would not be sure how to then use the locations where A is storing a 1 to call the values of the other layers of the array.
Could anyone offer some insight?
I am sure there is a really simple solution I am missing...
I would appreciate it.
0 commentaires
Réponse acceptée
José-Luis
le 10 Juin 2014
idx = M(:,:,1) == 1;
s1 = sum(sum(M(:,:,2).*idx));
s2 = sum(sum(M(:,:,3).*idx));
2 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Logical dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!