Finding column 2 values for column 1 value in a multidimensional array
Afficher commentaires plus anciens
I have a (:,2) array of data, where column 1 are x-values and column 2 are y-values.
I have a calculated y-value saved as a variable (like B shown below), and I want to:
(1) locate the y-values closest to my variable B and (2) extract the x-values that correspond to these y-values.
For the example below, I would want to find the y-values 0.11 and then extract the x-values 0.22 and 0.33 into an array.
Here is a simplified version of my issue:
A = [0.22 0.11; 0.33 0.11; 0.55 0.66]
A =
0.2200 0.1100
0.3300 0.1100
0.5500 0.6600
B = 0.12;
B1 = 0.12 + 0.01;
B2 = 0.12 - 0.01;
idx = find(A < B1 && A > B2);
I get this error: Operands to the || and && operators must be convertible to logical scalar values.
Can I not use variables when setting conditions for find? I am a MATLAB novice so any help would be much appreciated!
Réponse acceptée
Plus de réponses (1)
Image Analyst
le 13 Déc 2018
The comparisons A < B1 or A > B2 each product a logical vector. So you need to do an AND operation element by element with &. You used && which takes two scalar variables. So this should work:
indexes = find(A < B1 & A > B2);
You will now get linear indexes (not logical since were using the find function) where BOTH of those conditions are true.
2 commentaires
Diana Lutz
le 13 Déc 2018
Image Analyst
le 14 Déc 2018
Correct! With your data
A =
0.2200 0.1100
0.3300 0.1100
0.5500 0.6600
There is no element that is in the range 0.11 to 0.13 (non-inclusive), which would mean both less than 0.13 and greater than 0.11.
If you want to include the 0.11 you can use >= instead of >
indexes = find(A <= B1 & A >= B2)
Catégories
En savoir plus sur Data Type Conversion dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!