Constructing an if statement when a column contains only one data point and the rest are NaNs
1 回表示 (過去 30 日間)
古いコメントを表示
Dear all,
I have the double array
2.8000 0.2333 0.0010 0.0022
NaN NaN NaN NaN
NaN NaN NaN NaN
NaN NaN NaN NaN
NaN NaN NaN NaN
NaN NaN NaN NaN
NaN NaN NaN NaN
NaN NaN NaN NaN
NaN NaN NaN NaN
NaN NaN NaN NaN
NaN NaN NaN NaN
NaN NaN NaN NaN
NaN NaN NaN NaN
NaN NaN NaN NaN
NaN NaN NaN NaN
NaN NaN NaN NaN
NaN NaN NaN NaN
NaN NaN NaN NaN
NaN NaN NaN NaN
NaN NaN NaN NaN
NaN NaN NaN NaN
NaN NaN NaN NaN
NaN NaN NaN NaN
NaN NaN NaN NaN
NaN NaN NaN NaN
NaN NaN NaN NaN
NaN NaN NaN NaN
NaN NaN NaN NaN
NaN NaN NaN NaN
NaN NaN NaN NaN
NaN NaN NaN NaN
NaN NaN NaN NaN
NaN NaN NaN NaN
NaN NaN NaN NaN
NaN NaN NaN NaN
NaN NaN NaN NaN
I Want to see if each column contains at least two data points using an if statement.
For instance
for c=1:size(A,2)
if A(:,c) contains only one data point
'do that'
end
end
0 件のコメント
採用された回答
per isakson
2012 年 8 月 11 日
編集済み: per isakson
2012 年 8 月 11 日
This script prints
Column 2 has it
Column 3 has it
====
M = [
2.8000 0.2333 0.0010 0.0022
inf 17 17 NaN
NaN NaN 18 NaN
NaN NaN NaN NaN ];
has_two_or_more = sum(double(not(isnan(M)|isinf(M))), 1 ) >= 2;
col = 0;
for has = has_two_or_more
col = col + 1;
if has
fprintf( 'Column %d has it\n', col )
end
end
3 件のコメント
per isakson
2012 年 8 月 12 日
That's fine, however you abandon the condition "at least two data points". I prefer to change my loop to.
for col = 1 : size( M, 2 )
if has_two_or_more( col )
fprintf( 'Column %d has it\n', col )
end
end
It's about readability, testability and maybe a microsecond. I like to test the condition separately; outside the loop. "Hardcoding" the word "two" in the variable name was silly.
Image Analyst
2012 年 8 月 12 日
Well like I said in my response, he said "one data point" in the subject and then both "one" and "two data points" in the body of his message. He was unclear so that's why I made mine handle any number. It counts how many there are and then you can decide if you want 1 or 2 or whatever.
その他の回答 (2 件)
Image Analyst
2012 年 8 月 11 日
From your wording it's not clear whether you want to find "two data points" or "only one data point", since you say it both ways.
Try this:
A=[2.8000 0.2333 0.0010 0.0022
NaN NaN NaN NaN
NaN NaN NaN NaN
NaN NaN NaN NaN
NaN NaN NaN NaN
NaN NaN NaN NaN];
[rows columns] = size(A);
for c = 1 : columns
rowsWithNumbers = ~isnan(A(:,c));
numberOfNumbers = sum(rowsWithNumbers);
fprintf('Column #%d has %d numbers and %d NaNs\n',...
c, numberOfNumbers, rows - numberOfNumbers)
end
Results in command window:
Column #1 has 1 numbers and 5 NaNs
Column #2 has 1 numbers and 5 NaNs
Column #3 has 1 numbers and 5 NaNs
Column #4 has 1 numbers and 5 NaNs
0 件のコメント
Azzi Abdelmalek
2012 年 8 月 11 日
編集済み: Azzi Abdelmalek
2012 年 8 月 11 日
ind=sum( arrayfun(@(x) ~isnan(x),A))
for k=1:length(ind)
if ind(k)>=2
%do
end
end
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で NaNs についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!