How to index two vectors according to some condition

1 vue (au cours des 30 derniers jours)
Hamza  Makhamreh
Hamza Makhamreh le 1 Juin 2018
Commenté : Hamza Makhamreh le 1 Juin 2018
Hello friends, Let’s say I have two vectors with the same length
x=[ 1 -3 0 -7 5 7 0]
y=[ 9 -4 8 -9 4 1 8].
I want to find out the index (with respect to the two vectors) where both vectors (simultaneously) have closest negative value to zero. In this case, the index will be indx=2 (x=-3 && y=-4). In case if there is no negative values in both vectors like
x=[ 1 -8 0 8 -5 7 0]
y=[ 9 5 0 0 0 1 8]
I want to find where x has largest negative value and y has zero value. In this case indx=5 (x=-5 && y=0)
Hope this question is clear and I’ll appreciate your help.
  4 commentaires
Stephen23
Stephen23 le 1 Juin 2018
"I said largest negative value"
Generally in English the "largest negative value" would be considered to mean the negative value with the largest magnitude, which is how both Paolo and I understood it.
Hamza  Makhamreh
Hamza Makhamreh le 1 Juin 2018
Yeah I see. Anyways, thanks guys...

Connectez-vous pour commenter.

Réponse acceptée

the cyclist
the cyclist le 1 Juin 2018
I think this does what you want
% Case 1
x=[ 1 -3 0 -7 5 7 0];
y=[ 9 -4 8 -9 4 1 8];
% % Case 2
% x=[ 1 -8 0 8 -5 7 0];
% y=[ 9 5 0 0 0 1 8];
pairDistance = abs(x+y);
bothNegative = (x<0) & (y<0);
xNegAndYZero = (x<0) & (y==0);
if any(bothNegative)
[~,idx] = min(pairDistance./bothNegative);
else
[~,idx] = min(pairDistance./xNegAndYZero);
end
  1 commentaire
Hamza  Makhamreh
Hamza Makhamreh le 1 Juin 2018
Thank you sir. I expected that you are the one who will answer this question. Thanks...

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by