Filter löschen
Filter löschen

Selecting Specific Elements in a Matrix Using Group

5 Ansichten (letzte 30 Tage)
Dan
Dan am 26 Aug. 2022
Kommentiert: the cyclist am 31 Aug. 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

Akzeptierte Antwort

the cyclist
the cyclist am 26 Aug. 2022
Bearbeitet: the cyclist am 26 Aug. 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

Weitere Antworten (1)

rumin diao
rumin diao am 26 Aug. 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 Kommentar
the cyclist
the cyclist am 31 Aug. 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

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Tables finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by