Finding column 2 values for column 1 value in a multidimensional array
Mostrar comentarios más antiguos
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!
Respuesta aceptada
Más respuestas (1)
Image Analyst
el 13 de Dic. de 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 comentarios
Diana Lutz
el 13 de Dic. de 2018
Image Analyst
el 14 de Dic. de 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)
Categorías
Más información sobre Data Type Conversion en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!