Selecting Specific Elements in a Matrix Using Group

6 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

the cyclist
the cyclist il 26 Ago 2022
Modificato: the cyclist il 26 Ago 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

Più risposte (1)

rumin diao
rumin diao il 26 Ago 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 Commento
the cyclist
the cyclist il 31 Ago 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

Accedi per commentare.

Categorie

Scopri di più su Tables in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by