Reputation: 11
Suppose we have the following cell arrays that store the name and gender of subjects who participated in an experiment:
Names = { 'Amy' , 'Chris' , 'John' , 'Karin' , 'Juan' };
Gender = { 'Female' , 'Male' , 'Male' , 'Female' , 'Male' };
Suppose the experiment also measures response times for a number of trials. We store this information in the following matrix where each column corresponds to a person and each row corresponds to a trial in the experiment:
ResponseTimes = [ 2.4 2.2 2.9 2.7 2.7;
1.6 1.7 1.9 1.5 1.0;
2.1 2.2 0.9 2.5 1.9;
1.7 2.4 1.6 2.1 1.4 ];
How can I create a Matlab command that lists all male names using the strcmp
function?
Upvotes: 1
Views: 701
Reputation: 21563
Just a guess, you may also want the corresponding response times:
idx = strcmp(Gender, 'Male');
maleNames = Names(idx);
maleResponseTimes = ResponseTimes(:,idx);
For females use Names(~idx)
and ResponseTimes(:,~idx)
Upvotes: 0
Reputation: 9317
You can try
Names(strcmp(Gender, 'Male'))
which results in
ans =
'Chris' 'John' 'Juan'
Upvotes: 4