Q:

A script stores information on potential subjects for an experiment in a vector of structures called subjects. The following shows an example of what the contents might be:

0

>> subjects(1)

ans = 

 name: 'Joey'

 sub_id: 111

 height: 6.7000

 weight: 222.2000

For this particular experiment, the only subjects who are eligible are 

those whose height or weight is lower than the average height or 

weight of all subjects. The script will print the names of those who are 

eligible. Create a vector with sample data in a script, and then write 

the code to accomplish this. Don’t assume that the length of the vector

is known; the code should be general.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Ch8Ex20.m

% create vector of structures on potential subjects for an

% experiment, calculate ave height and weight, and

% determine and print eligible participants

subjects(3) = struct('name','Mary','sub_id',363,'height',5.1,...

'weight',110);

subjects(1) = struct('name','Joey','sub_id',111,'height',6.7,...

'weight',222.2);

subjects(2) = struct('name','Pat','sub_id',221,'height',5.9,...

'weight',165);

%calculate the average height and weight

avgheight = sum([subjects.height])/length(subjects);

avgweight = sum([subjects.weight])/length(subjects);

%find and print the eligible participants

disp('Eligible Subjects:')

for i = 1:length(subjects)

 if subjects(i).height < avgheight || subjects(i).weight < 

avgweight

 fprintf('%s\n',subjects(i).name)

 end

end

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now