Select values in one column based on entries in another column

72 vues (au cours des 30 derniers jours)
Turbulence Analysis
Turbulence Analysis le 21 Fév 2022
Commenté : Arif Hoq le 21 Fév 2022
Hi.
I looking for a way to read values in one column based on the entries in qnother column,
For example in the attached array A, the first entry in the first column is -3.14, here I would like to read all the correponding entries in the third column whichever got -3.14 in the first column..

Réponse acceptée

Arif Hoq
Arif Hoq le 21 Fév 2022
Modifié(e) : Arif Hoq le 21 Fév 2022
load matlab
[idx]=find(A(:,1)==-3.14)
B=A(:,3); % third column
Output=B(idx);
  2 commentaires
Turbulence Analysis
Turbulence Analysis le 21 Fév 2022
Thanks, it is perfect
Arif Hoq
Arif Hoq le 21 Fév 2022
my pleasure

Connectez-vous pour commenter.

Plus de réponses (1)

Arthur Reis
Arthur Reis le 21 Fév 2022
Modifié(e) : Arthur Reis le 21 Fév 2022
You can use a logical mask. For example (using two vectors, but it is easily adaptable for your case with arrays with many columns):
A = [1; 2; 3; -3.14; 4; -3.14];
A =
1.0000
2.0000
3.0000
-3.1400
4.0000
-3.1400
>> A_mask = (A==-3.14) % also works with <, >, >=, <=.....
A_mask =
6×1 logical array
0
0
0
1
0
1
>> B = ['a'; 'b'; 'c'; 'd'; 'e'; 'f']
B =
6×1 char array
'a'
'b'
'c'
'd'
'e'
'f'
>> B(A_mask) %passing a logical array as indexes will return every 'true' element
ans =
2×1 char array
'd'
'f'

Catégories

En savoir plus sur Matrices and Arrays 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