# How to pick next value from vectors?

66 views (last 30 days)
Teemu on 4 Mar 2013
Edited: shariq khan on 3 Dec 2018
I have a cumulative vector, size is about 200. I want to pick up that index where value is 0,5 or value which is next to it. For example (0.1, 0.23, 0.42, 0.49, 0.52, 0.56,...), so i want to pick up index which corresponding to value 0.52. How can I do it?
Teemu on 5 Mar 2013
actually I need both

Jos (10584) on 4 Mar 2013
Here is one approach:
v = [0.1 0.23 0.4 0.52 0.56 1.1]
idx = find(v >= 0.5,1,'first')
v(idx)
Teemu on 5 Mar 2013
Sorry I didn't noticed that, I thought it was answer for next index. Thanks for your patience.

Jos (10584) on 5 Mar 2013
To do this for multiple values, take a look at my function NEARESTPOINT:

Jan on 4 Mar 2013
Edited: Jan on 5 Mar 2013
v = [0.1 0.23 0.4 0.52 0.56 1.1];
[absdist, nearest] = min(abs(v - 0.5)); % **SOLUTION IS HERE** !!!
if absdist == 0 % or: <= eps(v(index))
following = index;
else
following = index + 1;
end
nearestValue = value(nearest);
followingValue = value(following);
##### 2 CommentsShowHide 1 older comment
shariq khan on 3 Dec 2018
Edited: shariq khan on 3 Dec 2018
I think I found answer to my problem
Problem - all vectors are of same length
x = [some values]
t = [some values]
x1 = 0.9*max(x);
find t value that corresponds to x1 value
if not find the closest value
solution : A lot from @jan ans
t1(that is to be determined) = t(index);
I hope it helps to solve people facing specific problem