Finding integers in an array.

30 次查看(过去 30 天)
NS
NS 2011-6-10
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

采纳的回答

Matt Fig
Matt Fig 2011-6-10
V = [nan;nan;9;nan;6;7;nan;9] % An example to work with...
idx = find(V==V,1); % Location. nan never equals nan...
val = V(idx); % Value at Location.
  7 个评论
Jan
Jan 2013-1-11
@Ricardo: Please do not post a question as a comment to an answer of another question. New questions need a new thread. Thanks.
@Andrei: eps(100) might be wrong, if the magnitude of the data is 1e8. Either eps(M) could be better, or:
find(M==floor(M))
Ricardo Pereira
Ricardo Pereira 2013-1-11
Jan Siman: Sorry. You're right. I should have posted this question on another post.
Your answer works for me.
R = find(M==floor(M))
Thanks a lot

请先登录,再进行评论。

更多回答(2 个)

Andrei Bobrov
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 个评论
Andrei Bobrov
Andrei Bobrov 2011-6-10
Matt! You're right as always
NS
NS 2011-6-10
Thanks for the help Matt and Andrei.
Andrei, your code works well. I dont know what I did wrong yesterday. I dont think I can accept two answers at the same time here. :(
Thanks for the help once again.

请先登录,再进行评论。


Yella
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
NS 2011-6-10
Thanks for the help Yella. :)
Matt Fig
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by