using logical mask in array

hello! i have this array: C=[1 1 1 2 3 4 5 5 5 5 6 6 6 6 ] and i wana to get the index of array where C=5, i can use res=find(C==5), but i went to use logical masks to improve performance in my code, so can any one tel me how i use logical mask ?? ^^ thanks

Respuestas (1)

Image Analyst
Image Analyst el 14 de Ag. de 2017

6 votos

Mask = C==5;

5 comentarios

Stephen23
Stephen23 el 14 de Ag. de 2017
khaled nouri's "Answer" moved here:
but Mask=C==5 gives % 0 0 0 0 0 0 1 1 1 1 0 0 0 0 and i wana ; 7 8 9 10 => the idex of element with equales to 5 ??
Jan
Jan el 14 de Ag. de 2017
@khaled: You asked for the logical mask explicitly. The logical mask is
C == 5 % [0 0 0 0 0 0 1 1 1 1 0 0 0 0]
In opposite to this [7, 8, 9, 10] are the indices. As you have found out already, logical indexing is faster in general, because Matlab has to check the length of the index vector only, while for the list of indices, each value must be checked to be inside the limits.
But khaled, that's what you had originally - the linear indexes, the actual index numbers. You said you wanted logical (true/false) indexes so that's what I gave you. I hope you can understand the illustrative code below:
>> C=[1 1 1 2 3 4 5 5 5 5 6 6 6 6 ]
C =
1 1 1 2 3 4 5 5 5 5 6 6 6 6
>> linearIndexes = find(C == 5)
linearIndexes =
7 8 9 10
>> logicalIndexes = C == 5 % Can be used as a mask
logicalIndexes =
1×14 logical array
0 0 0 0 0 0 1 1 1 1 0 0 0 0
Image Analyst
Image Analyst el 15 de Ag. de 2017
khaled's "Answer" moved here:
Yes, thank very much you for the illustration.
Image Analyst
Image Analyst el 15 de Ag. de 2017
So, if we're done then could you "Accept" the answer? Otherwise explain what you still need help on.

Iniciar sesión para comentar.

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Preguntada:

el 14 de Ag. de 2017

Comentada:

el 15 de Ag. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by