Search code examples
arraysmatlabindices

find common indices that satisfy 2 conditions in matlab


I have an array that is 2D and quite large(8000x6000). There are two conditions that are been satisfied for the same array (cond1 and cond2) and i want to reconcile them, find the common indices that satisfy both conditions

F=rand(8000,6000);
ind1=find(F>0.5);ind1 stores indices that satisfy cond1

The second condition (cond2) is whether the indices satisfy a condition.

newF=zeros(8000,6000);
[x,y]=meshgrid(1:6000,1:8000);
newF(x+y>200)=1;

The new array newF has zero values when the new condition is not satisfied, while the value is one when the condition is satisfied.

I want to find the common indices for F and newF that satisfy both conditions. When i tried to find the ind2(r,c)

 [r,c]=find(newF>0) 

I could not reconcile the ind1 and r,c to find the common indices. Can someone help me out?


Solution

  • Multiply logical matrices, obtained by applying the conditions, element wise and then use find to find common rows and column subscripts. i.e.

    [r, c] = find((F>0.5) .* (newF>0));    
    % .* is also replaceable by &. Use whichever is faster