How to find the lowest and highest rows in a column vector which contain a value.

1 回表示 (過去 30 日間)
Nathan B
Nathan B 2018 年 1 月 31 日
編集済み: Nathan B 2018 年 1 月 31 日
So, I have a 20x1 vector (A) shown below. How would I find the row number for the lowest and highest row that contain a value? In the vector below the lowest would be row 6, and the highest would be row 17. Thanks.
A =
NaN
NaN
NaN
NaN
NaN
1
1
1
NaN
NaN
NaN
1
1
1
NaN
NaN
1
NaN
NaN
NaN

採用された回答

Akira Agata
Akira Agata 2018 年 1 月 31 日
idx = ~isnan(A);
lowestRow = min(find(idx));
highestRow = max(find(idx));
  5 件のコメント
Nathan B
Nathan B 2018 年 1 月 31 日
編集済み: Nathan B 2018 年 1 月 31 日
For the code below what would I put where I have written "here" so that the if statement only runs when A is not NaN?
for y = lowestRow:1:highestRow
if "here"
disp(A(y))
end
end
Jos (10584)
Jos (10584) 2018 年 1 月 31 日
You also forgot the for-loop counter
for K = lowestRow:highestRow
if ~isnan(A(K))
disp(K)
end
end
However,
tf = ~isnan(A)
disp( A(tf) )
would almost do the same and is much more efficient

サインインしてコメントする。

その他の回答 (1 件)

Jos (10584)
Jos (10584) 2018 年 1 月 31 日
idx = ~isnan(A);
lowestRow = find(idx, 1, 'first')
highestRow = find(idx, 1, 'last')
  1 件のコメント
Nathan B
Nathan B 2018 年 1 月 31 日
編集済み: Nathan B 2018 年 1 月 31 日
Thanks. Also thanks for helping with the if statement.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by