Finding integers in an array.
이전 댓글 표시
Hey Guys,
I have an column matrix that basically consists of NaNs and some integers in between them. Are there any functions in MATLAB that will help me find 1. the first integer value in the array, 2. its location 3. store the value
Thanks, NS
채택된 답변
추가 답변 (2개)
Andrei Bobrov
2011년 6월 10일
l = isnumeric(V)&rem(V,1)==0;
ii = find(l,1,'first'); % 2
ValInt = V(ii); % 1 and 3
댓글 수: 5
NS
2011년 6월 10일
Matt Fig
2011년 6월 10일
andrei's code finds the correct ii (6) and ValInt (34) when I run the code...
Matt Fig
2011년 6월 10일
BTW andrei, I think you are meaning the call to ISNUMERIC to act as a nan filter? Your code works just fine with only a call to REM.
Andrei Bobrov
2011년 6월 10일
Matt! You're right as always
NS
2011년 6월 10일
Yella
2011년 6월 10일
%An example to work with x = [NaN 1.7 1.6 1.5 NaN 1.9 1.8 1.5 5.1 1.8 Inf 1.4 2.2 1.6 1.8];
for i=1:1:length(x)
if (isnan(x(i)))
continue;
else
p=i;
break;
end
end
x_withno_nan = x(isfinite(x))
x_first=x_withno_nan(1)
It may be length but i made easily understandable for a beginner like me.
댓글 수: 2
NS
2011년 6월 10일
Matt Fig
2011년 6월 10일
Yella, there is nothing wrong with using a FOR loop for this problem!
I would only add that your code can be simplified:
for ii = 1:numel(x)
if floor(x(ii))==x(ii) % Only integers - use x(ii)==x(ii) or ~isnan(x(ii))
idx = ii; % The index of the first non-nan
val = x(ii); % The value at idx.
break
end
end
카테고리
도움말 센터 및 File Exchange에서 Data Type Identification에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!