Effacer les filtres
Effacer les filtres

Selecting Specific Elements in a Matrix Using Group

5 vues (au cours des 30 derniers jours)
Dan
Dan le 26 Août 2022
Commenté : the cyclist le 31 Août 2022
Hello, I am trying to select elements only from a single group from my data.
Let's say my current code is:
Height = [62; 65; 63; 70; 58; 70; 73]
Gender = [1; 1; 1; 2; 1; 2; 2] % where 1 is female and 2 is male
data_table = [Height(:), Gender(:)]
From the table, I would like to separate the male and female heights and put them into their own separate vectors so I can perform a two-sample t-test. Can anyone help me with this?
Thank you

Réponse acceptée

the cyclist
the cyclist le 26 Août 2022
Modifié(e) : the cyclist le 26 Août 2022
Height = [62; 65; 63; 70; 58; 70; 73];
Gender = [1; 1; 1; 2; 1; 2; 2]; % where 1 is female and 2 is male
data_table = [Height(:), Gender(:)];
Height_F = Height(Gender==1)
Height_F = 4×1
62 65 63 58
Height_M = Height(Gender==2)
Height_M = 3×1
70 70 73
or
Height_F = data_table(data_table(:,2)==1,1)
Height_F = 4×1
62 65 63 58

Plus de réponses (1)

rumin diao
rumin diao le 26 Août 2022
you can use the function 'find' to locate different gender:
%current code
Height = [62; 65; 63; 70; 58; 70; 73];
Gender = [1; 1; 1; 2; 1; 2; 2]; % where 1 is female and 2 is male
data_table = [Height(:), Gender(:)];
%seperate female
female = find(Gender == 1);
heightOfFemale = Height(female);
male = find(Gender == 2);
heightOfMale = Height(male);
  1 commentaire
the cyclist
the cyclist le 31 Août 2022
find is a waste of code here, because logical indexing will handle it (which is exactly what my solution did):
%current code
Height = [62; 65; 63; 70; 58; 70; 73];
Gender = [1; 1; 1; 2; 1; 2; 2]; % where 1 is female and 2 is male
%seperate female
female = (Gender == 1);
heightOfFemale = Height(female)
heightOfFemale = 4×1
62 65 63 58
male = (Gender == 2);
heightOfMale = Height(male)
heightOfMale = 3×1
70 70 73

Connectez-vous pour commenter.

Catégories

En savoir plus sur Tables 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