How to make vector of Unicode characters align with 1x2 matrix of numbers

조회 수: 2 (최근 30일)
I have the following code:
playerCards = [2 1];
allSuites = [9824 9827 9829 9830];
% all possible unicode values for bold card suites
dispplayerCards = [];
% initialising vector to display card suite (random) with given number
storeUnicode = [];
for i = 1:length(playerCards)
randSuite = randsample(allSuites,1);
% selects random suite unicode number
storeUnicode = [char(randSuite) storeUnicode];
% stores random unicode suite symbol in the vector
end
disp(storeUnicode);
disp(playerCards);
When the results print in the command window, this is what I can see:
♦♦
2 1
Is there any way to make the output such as this:
2 1
such that each element of storeUnicode array is placed directly above each element of playerCards array?
Thank you.
  댓글 수: 2
Walter Roberson
Walter Roberson 2022년 4월 26일
Nothing in that code prevents the same card from being generated multiple times. Suppose that the player cards were 3 5 3 then you generate a random suit for the 3 but 1/4 of the time you will generate the same suit for the second 3.
Aditya Sajeesh
Aditya Sajeesh 2022년 4월 26일
Walter I did not realise about this, I will take this into consideration when I am making my final project file.

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

채택된 답변

Davide Masiello
Davide Masiello 2022년 4월 26일
편집: Davide Masiello 2022년 4월 27일
clear,clc
playerCards = [];
storeUnicode = [];
allSuites = [9824 9827 9829 9830]; % all possible unicode values for bold card suites
for k = 1:5
playerCards = [playerCards,randi(10,1,1)];
randSuite = randsample(allSuites,1); % selects random suite unicode number
storeUnicode = [storeUnicode,char(randSuite)]; % stores random unicode suite symbol in the vector
fprintf([repmat(' %c ',1,k),'\n',repmat(' %d ',1,k),'\n\n'],[storeUnicode,playerCards])
end
♣ 1 ♣ ♣ 1 6 ♣ ♣ ♠ 1 6 4 ♣ ♣ ♠ ♥ 1 6 4 9 ♣ ♣ ♠ ♥ ♠ 1 6 4 9 6
  댓글 수: 4
Aditya Sajeesh
Aditya Sajeesh 2022년 4월 27일
Thank you Davide, for all your help, this is what I was struggling with.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by