Replacing a for loop with logical indexing

5 ビュー (過去 30 日間)
Tyler
Tyler 2016 年 4 月 28 日
コメント済み: Tyler 2016 年 4 月 28 日
If someone could help me out with this simple problem, I would really appreciate it. I'm still trying to wrap my head around indexing. I have a 5x5 array [A], and I would like have a logical 1x5 array B, where B indicates the columns of A that have 3 or more NaN values. So far I've been able to make a for loop, but I would like to simplify this if possible to a logical index.
A = [1,2,3,4,5;2,3,4,5,6;NaN,NaN,NaN,NaN,NaN;NaN,NaN,NaN,4,NaN;NaN,NaN,7,8,NaN];
B = zeros(1,5);
for i = 1:5
if numel(find(isnan(A(:,i)))) > 2
B(i) = 1;
end
end
(output) B = [1,1,0,0,1]
Thank you for your time.

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2016 年 4 月 28 日
編集済み: Azzi Abdelmalek 2016 年 4 月 28 日
out=sum(isnan(A))>=3
  1 件のコメント
Tyler
Tyler 2016 年 4 月 28 日
Thank you!

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

その他の回答 (1 件)

Jon
Jon 2016 年 4 月 28 日
B = sum(isnan(A))>2;
  1 件のコメント
Tyler
Tyler 2016 年 4 月 28 日
Thanks!

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by