Constructing an if statement when a column contains only one data point and the rest are NaNs
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Akzeptierte Antwort
per isakson
am 11 Aug. 2012
Bearbeitet: per isakson
am 11 Aug. 2012
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 Kommentare
per isakson
am 12 Aug. 2012
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
am 12 Aug. 2012
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.
Weitere Antworten (2)
Image Analyst
am 11 Aug. 2012
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 Kommentare
Azzi Abdelmalek
am 11 Aug. 2012
Bearbeitet: Azzi Abdelmalek
am 11 Aug. 2012
ind=sum( arrayfun(@(x) ~isnan(x),A))
for k=1:length(ind)
if ind(k)>=2
%do
end
end
0 Kommentare
Siehe auch
Kategorien
Mehr zu NaNs finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!