Printing data results with Fprintf

조회 수: 1(최근 30일)
azaandria
azaandria 2021년 3월 23일
댓글: azaandria 2021년 3월 23일
Hello,
I am trying to display the event names that each gymnast won, not the event #. Could someone please explain how to do this? My output is down at the very bottom.THank you!
events={'Pommel Horse ';
'Vault ';
'Floor ';
'Rings ';
'High Bar ';
'Parallel Bars'};
g1=input('Enter scores for Gymnast 1:')
g2=input('Enter scores for Gymnast 2:')
table=[events; g1; g2];
disp(' ');
disp('Number of events won by Gymnast 1:')
g1won=find(g1>g2);
num_of_wins=numel(g1won);
disp(num_of_wins);
disp('Event #s won by Gymnast 1:');
disp(g1won);
disp(' ');
disp('Number of events won by Gymnast 2:');
g2won=find(g2>g1);
numb_of_wins=numel(g2won);
disp(numb_of_wins);
disp('Event #s won by Gymnast 2:');
disp(g2won);
disp('Gymnast 1 Average Score:');
A1=mean(g1);
disp(A1);
disp('Gymnast 2 Average Score:');
A2=mean(g2);
disp(A2);
disp('Overall winner:');
if(A1==A2)
disp('Tie breaker')
elseif(A1>A2)
disp('Gymnast 1')
else
disp('Gymnast 2')
end
%
%
%Output
%Enter scores for Gymnast 1:[9.921 9.923 9.729 9.908 9.534 9.973]
%Enter scores for Gymnast 2:[9.600 9.925 9.831 9.987 9.354 9.879]
%
%Number of events won by Gymnast 1:
% 3
%
%Event #s won by Gymnast 1:
% 1 5 6
%
%
%Number of events won by Gymnast 2:
% 3
%
%Event #s won by Gymnast 2:
% 2 3 4
%
%Gymnast 1 Average Score:
% 9.8313
%
%Gymnast 2 Average Score:
% 9.7627
%
%Overall winner:
%Gymnast 1

채택된 답변

Geoff Hayes
Geoff Hayes 2021년 3월 23일
azaandria - try replacing
disp('Event #s won by Gymnast 2:');
disp(g2won);
with
disp('Event #s won by Gymnast 2:');
eventsWonByGymnast2 = events(g2won);
fprintf('%s\n',eventsWonByGymnast2{:})
to get the event names rather than the event indices.
  댓글 수: 1
azaandria
azaandria 2021년 3월 23일
Thank you so much. It worked perfectly!

댓글을 달려면 로그인하십시오.

추가 답변(0개)

태그

Community Treasure Hunt

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

Start Hunting!

Translated by