MATLAB Answers

Translated by

このページのコンテンツは英語から自動翻訳されています。自動翻訳をオフにする場合は「<a class="turn_off_mt" href="#">ここ</a>」をクリックしてください。

0

Find index of first zero searching from left first column first row, then find index of first zero searching from last column last row

monkey_matlab さんによって質問されました 2017 年 10 月 22 日
最新アクティビティ Cedric Wannaz
さんによって 編集されました 2017 年 10 月 22 日
Hello,
In the code below, I am attempting to find the index of first zero searching from the left first column first row, going down the column, then find the index of first zero from last column last row searching up (as shown in the arrows below)?
In the picture above, rstart = 1, cstart = 1, rstop = 10, cstop = 20.
How do I go about accomplishing this task.
This is what I have so far:
n = 10;
m = 20;
M = randi([0 1], n,m);
for i = 1:1:n
for j = 1:1:m
if M(i,j) == 0
rstart = i;
cstart = j;
break
end
end
end
for ii = n:-1:1
for jj = m:-1:1
if M(ii,jj) == 0
rstop = ii;
cstop = jj;
break
end
end
end

  0 件のコメント

サインイン to comment.

タグ

製品

1 件の回答

回答者: Cedric Wannaz
2017 年 10 月 22 日
編集済み: Cedric Wannaz
2017 年 10 月 22 日
 採用された回答

MATLAB stores data column first in memory. Accessing your 2D array linearly follows this structure. Evaluate
>> M(:)
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

  0 件のコメント

サインイン to comment.



Translated by