Using find function output locations to sum over values from another matrix.

3 visualizaciones (últimos 30 días)
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.

Respuesta aceptada

José-Luis
José-Luis el 10 de Jun. de 2014
idx = M(:,:,1) == 1;
s1 = sum(sum(M(:,:,2).*idx));
s2 = sum(sum(M(:,:,3).*idx));

Más respuestas (0)

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by