How to get next higher value from vector in Matlab ?
26 次查看(过去 30 天)
显示 更早的评论
Hi How can I get next higher index of next higher value from vector ?
For example , I have vector A = [0 1 5 -1 4 6],
In above vector , if I start from index 1 , the next higher value is 5(index 3).
If I start from index 4, the next higher value is 6 (index 6).
Regards,
Koustubh
7 个评论
Walter Roberson
2020-12-29
If 1 is an index, then why isn't the expected output 2 (the index of the first location after position 1 that contains a value greater than the value stored at position 1) -- or if not the index, the value stored there, namely 1.
A(2) > A(1) so if the input is the index 1, then either index 2 or the content of A(2) should be the answer.
If input 1 is a value then either index 3 or the content of A(3) would make sense.
If input 4 is an index then A(4) = -1 and A(5) > A(4) so either 5 or the content of A(5) should be the answer.
If input 4 is a value then 6 or the content of A(6) could make sense as being a higher value after the value 4
B = [0 0 1 2 4 -7 20 -9 -1 0];
If 3 is an index then A(4) > A(3) so either 4 or the content of B(4) would make sense -- but this approach would contradict the previous decisions that said that A(2) is not the right result for input 1.
If 3 is a value then B(5) > 3 so either 5 or B(5)=4 could make sense as a higher value than 3. But this is not the same decision as for the A 4 case: If you are looking for the first element in the vector that is greater than the given value, then for A you would expect to return the information corresponding to the value 5. If you are working with values, then you need to decide if you want the first value starting at the beginning of the vector that is greater than the given value, or if you want the first value starting at the given value in the vector that is greater than the given value -- in which case there should be no result for B and 3 since 3 does not occur in B.
Again, for your A and B, I need to ask: what input "index" would you give if you wanted to inquire about -1, seeing as -1 is not a valid index for an array?
采纳的回答
Walter Roberson
2020-12-29
A = [0 1 5 -1 4 6];
first_higher(A, 1)
first_higher(A, 4)
first_higher(A, 5)
function fight = first_higher(A, target)
targmask = ~cumprod(A ~= target);
highermask = (A > target) & targmask;
fight = A(find(highermask,1));
end
更多回答(1 个)
Bruno Luong
2020-12-29
编辑:Bruno Luong
2020-12-29
B = [0 0 1 2 4 -7 20 -9 -1 0];
d=diff([-Inf B -Inf],1);
ip=find(d(1:end-1)>=0 & d(2:end)<=0);
for idx=1:length(B)
hindex = ip(find(ip>idx,1,'first'));
hvalue = B(hindex);
fprintf('idx=%d, hindex=%d, value=%g\n', idx, hindex, hvalue);
end
You'll get
idx=1, hindex=5, value=4
idx=2, hindex=5, value=4
idx=3, hindex=5, value=4
idx=4, hindex=5, value=4
idx=5, hindex=7, value=20
idx=6, hindex=7, value=20
idx=7, hindex=10, value=0
idx=8, hindex=10, value=0
idx=9, hindex=10, value=0
idx=10, hindex=, value=
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!