How to get next higher value from vector in Matlab ?
Show older comments
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 Comments
Walter Roberson
on 29 Dec 2020
?? If you start with index 1, then the associated value is 0, and the next higher value would be at index 2, value 1
If you start from index 4, then the associated value is -1, and the next higher value would be at index 1, value 0
Koustubh Shirke
on 29 Dec 2020
Edited: Koustubh Shirke
on 29 Dec 2020
Walter Roberson
on 29 Dec 2020
The functionality is not properly defined at this point.
Are you sure you want to start at a particular index and not a particular value ? Each of the examples you showed involved starting with a particular value . 5 is the next higher value in the array after value 1. 6 is the next higher value in the array after value 4.
Koustubh Shirke
on 29 Dec 2020
Walter Roberson
on 29 Dec 2020
If you start with index 1, then the associated value is 0 and the next higher value is 1 at index 2.
If you start with index 4, then the associated value is -1 and the next higher value is 4 at index 5.
You need to decide on what you want:
- input is a value and you want the next higher value
- input is an index and you look at the value that is stored there and you find the next higher value
- input is both an index and a value, and you want to find the next higher value than the given value, but you want to start the search at the location treating that value as an index.
What would you expect the input to be if the value you wanted to search was -1 ?
Koustubh Shirke
on 29 Dec 2020
Walter Roberson
on 29 Dec 2020
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?
Accepted Answer
More Answers (1)
Bruno Luong
on 29 Dec 2020
Edited: Bruno Luong
on 29 Dec 2020
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=
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!