MATLAB: How to use the find function to search a range or rows and a specific column please

arrayfindMATLAB

Hey guys, so basically I'm trying to use the 'find' function in matlab to search a range of rows and a particular column in an array for the indices of elements that have a value of 7. So basically what I did was:
X=zeros(4,5);
X(1:3,4)=7;
[A,B] = find( X( 1:3,4) == 7 );
What the code is meant to do is search rows 1:3 of column 4 for elements with a value of seven and save the indices of these elements to A(i.e. rows) and B(i.e. columns). A works out fine as I get the values 1,2,3 stored in A but the values gotten in B are 1,1,1. Any idea of what I may be doing wrong please? I know I could achieve this using a for loop but I do not want to as this made my code really slow. Thanks

Best Answer

You are applying find to a 3 x 1 vector, and asking for row and column outputs. Since the vector is only one column wide, the output is always going to reflect column 1.
If you want to search a subarray X(J:K, P:Q)
[A,B] = find(X(J:K, P:Q) == 7)
but you want to get back indices relative to the original array, then you need A + J + 1 and B + P - 1
Related Question