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?

댓글 수: 3

Jan
Jan 2013년 3월 4일
편집: Jan 2013년 3월 4일
Does "next" mean the next larger element or the element with the minimal distance? In the 2nd case, the answer would be 0.49, in opposite to your example.
Teemu
Teemu 2013년 3월 5일
Minimal distance, in this case it will be 0.49
Teemu
Teemu 2013년 3월 5일
actually I need both

댓글을 달려면 로그인하십시오.

 채택된 답변

Jos (10584)
Jos (10584) 2013년 3월 4일

1 개 추천

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)

댓글 수: 8

Wayne King
Wayne King 2013년 3월 4일
@Jos, You're absolutely right Jos, I misread the poster's question. My answer is not relevant, deleting.
Jan
Jan 2013년 3월 4일
편집: Jan 2013년 3월 4일
The new version of your answer is the most clean and efficient solution. +1
(or "cleanest and most efficient"?)
Teemu
Teemu 2013년 3월 5일
It works fine, thanks. I have one extra question; can I pick value which is closest to value 0.5?
Teemu
Teemu 2013년 3월 5일
I mean can I pick index which value is closest to 0.5?
Jan
Jan 2013년 3월 5일
@Teemu: See index in the code I've posted yesterday already.
Teemu
Teemu 2013년 3월 5일
So how can I do minimal distance pick?
Jan
Jan 2013년 3월 5일
Please, Teemu, read my answer below. There you find the code to pick the element with the minimal distance. It is the 2nd line and the index is the 2nd output of min(abs(v - 0.5)).
Teemu
Teemu 2013년 3월 5일
Sorry I didn't noticed that, I thought it was answer for next index. Thanks for your patience.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Jos (10584)
Jos (10584) 2013년 3월 5일

1 개 추천

To do this for multiple values, take a look at my function NEARESTPOINT:
Jan
Jan 2013년 3월 4일
편집: Jan 2013년 3월 5일

0 개 추천

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

shariq khan
shariq khan 2018년 12월 3일
what if I need next larger value? consider same data but my value at specific index is nil (or no value from dataset) but now I want to find if there is any value next to that specific value in the dataset
shariq khan
shariq khan 2018년 12월 3일
편집: shariq khan 2018년 12월 3일
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
[answer,index] = min(abs(x - x1))
t1(that is to be determined) = t(index);
I hope it helps to solve people facing specific problem

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

질문:

2013년 3월 4일

편집:

2018년 12월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by