How to get next higher value from vector in Matlab ?

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

?? 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
I hope you understood my question. Yes, @walter. I am lookign for fucntion or some tips fro scripting .
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.
I want to start from specific index , If I start with index 1 , then I should get the index of next higher value i.e index 3 (value 5)
If i start with index four I should get index of next higher value (6) which is 6 ( index is also 6).
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 ?
Lets make it in a simple way, I give any inputs as a index , as an output I would need a index of next 1st higher value.
in above example,
input : 1, output 3 (both are indexs)
Input : 4 , output : 6 (both are indexs)
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?

Sign in to comment.

 Accepted Answer

A = [0 1 5 -1 4 6];
first_higher(A, 1)
ans = 5
first_higher(A, 4)
ans = 6
first_higher(A, 5)
ans = 6
function fight = first_higher(A, target)
targmask = ~cumprod(A ~= target);
highermask = (A > target) & targmask;
fight = A(find(highermask,1));
end

1 Comment

Thanks walter. Lets take following example.
B = [0 0 1 2 4 -7 20 -9 -1 0];
If I want highest value from index 3,
>> first_higher(B,3) ... it should give 4 value but its giving empty answer
>> first_higher(B,6) ... it should give 20 value but its giving empty answer

Sign in to comment.

More Answers (1)

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=

Products

Release

R2020a

Community Treasure Hunt

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

Start Hunting!