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 件)

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by