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

 Accepted Answer

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 Comments

NS
NS on 10 Jun 2011
Thanks Matt.
It works :)
Good evening, Matt! Great solution "V==V", but with V = [NaN 34.15 54 93 NaN NaN], returns 34.15 as well must be 54.
Hello Andrei,
I trust that NS _knows_ the arrays are as they were described: nans and integers. If they are not, then you are correct that another check would be necessary...
Ricardo Pereira
Ricardo Pereira on 11 Jan 2013
Edited: Ricardo Pereira on 11 Jan 2013
Hello. I have a similar problem. I need basically the same thing, but I need save in one array of the workspace the position of all Integers (and only integers) of one array that contain integers and non integers.
Ex:
The column matrix like this
M = [0 0.2 0.4 5 6.3 7.5 8 9.4 11.8 13]
And the result will be the Integer position in a matrix
R = [1 4 7 10]
Anyone can help me?
Thanks
R = find(abs(rem(M,1)) < eps(100));
@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))
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

Sign in to comment.

More Answers (2)

l = isnumeric(V)&rem(V,1)==0;
ii = find(l,1,'first'); % 2
ValInt = V(ii); % 1 and 3

5 Comments

NS
NS on 10 Jun 2011
Thanks for the reply Andrei. I am not getting the required solution with this though.
Say my matrix is of form V=[NaN NaN NaN NaN NaN 34 54 67 89 93 NaN NaN]
I want to find the location and value of the first integer in the matrix i.e. of the number 34.
andrei's code finds the correct ii (6) and ValInt (34) when I run the code...
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.
Matt! You're right as always
NS
NS on 10 Jun 2011
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.

Sign in to comment.

%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 Comments

NS
NS on 10 Jun 2011
Thanks for the help Yella. :)
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

Sign in to comment.

Categories

Asked:

NS
on 10 Jun 2011

Community Treasure Hunt

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

Start Hunting!