Petrified
Petrified

Reputation: 11

strcmp function in matlab

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

Answers (2)

Dennis Jaheruddin
Dennis Jaheruddin

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

H.Muster
H.Muster

Reputation: 9317

You can try

Names(strcmp(Gender, 'Male'))

which results in

ans = 
    'Chris'    'John'    'Juan'

Upvotes: 4

Related Questions