MATLAB stores data column first in memory. Accessing your 2D array linearly follows this structure. Evaluate
and you will see a column vector that represents the content of the array read column per column. This means that you can FIND the linear index of these 0s using the 'first' (default) or 'last' options of FIND, by operating on M(:):
>> M = randi( 10, 4, 5 ) > 5
M =
4×5 logical array
1 1 1 1 0
1 0 1 0 1
0 0 0 1 1
1 1 1 0 1
>> linId = find( M(:) == 0, 1 )
linId =
3
>> linId = find( M(:) == 0, 1, 'last' )
linId =
17
What remains is to convert linear indices back to subscripts, and you can do this using IND2SUB. For example:
>> [r,c] = ind2sub( size( M ), find( M(:) == 0, 1, 'last' ))
r =
1
c =
5
Best Answer