How to get next higher value from vector in Matlab ?

23 vues (au cours des 30 derniers jours)
Koustubh Shirke
Koustubh Shirke le 29 Déc 2020
Commenté : Walter Roberson le 29 Déc 2020
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 commentaires
Koustubh Shirke
Koustubh Shirke le 29 Déc 2020
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)
Walter Roberson
Walter Roberson le 29 Déc 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?

Connectez-vous pour commenter.

Réponse acceptée

Walter Roberson
Walter Roberson le 29 Déc 2020
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 commentaire
Koustubh Shirke
Koustubh Shirke le 29 Déc 2020
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

Connectez-vous pour commenter.

Plus de réponses (1)

Bruno Luong
Bruno Luong le 29 Déc 2020
Modifié(e) : Bruno Luong le 29 Déc 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=

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Produits


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by