I'm trying to display a 2D table

조회 수: 6 (최근 30일)
Moustafa Ismail
Moustafa Ismail 2022년 10월 4일
댓글: Moustafa Ismail 2022년 10월 4일
Hi,
i'm trying to display a 2d table for a school projet, and i'm having trouble making it work, the code seem fine to me but I can't see to find how do I call the specific case I want to display.
my code is :
for ii=1:taille
if(ii==1)
fprintf('\t%g\t\t\t\t',tableau(ii));
elseif(ii==2)
fprintf('%g\t\t\t\t',tableau(ii));
elseif(ii==3)
fprintf('%g\t\t\t\t',tableau(ii));
elseif(ii==4)
fprintf('%g\t\t\t\t\t\t\n',tableau(ii));
end
end
fprintf('\n');
fprintf('\n');
%affichage de la deuxième ligne du tableau 2D
for jj=1:taille
if(jj==1)
fprintf('\t%g\t\t\t\t',tableau(jj));
elseif(jj==2)
fprintf('%g\t\t\t\t',tableau(jj));
elseif(jj==3)
fprintf('%g\t\t\t\t',tableau(jj));
elseif(jj==4)
fprintf('%g\t\t\t\t\t\t\n',tableau(jj));
end
end
and the table displays this when i call : afficher_ecran_de_jeu([1,2,3,4;5,6,7,8])
Carte 1 Carte 2 Carte 3 Carte 4
--------- --------- --------- ---------
1 5 2 6
1 5 2 6
when it supposed to display
1 2 3 4
5 6 7 8
thank you in advance. (PS: I started learning MATlab only 4 weeks ago).

채택된 답변

Benjamin Thompson
Benjamin Thompson 2022년 10월 4일
To read the rows and columns of a two dimensional table you need to use two indices:
In your example:
tableau(1,1) == 1
tableau(1,2) == 2
tableau(1,3) == 3
tableau(1,4) == 4
tableau(2,1) == 5
tableau(2,2) == 6
tableau(2,3) == 7
tableau(2,4) == 8
When you only use one index, MATLAB defaults to the row first data order in order to convert to a one dimensional vector, so you get 1, 5, 2, 6, 3, 7, 4, 8.
  댓글 수: 1
Moustafa Ismail
Moustafa Ismail 2022년 10월 4일
Thank you very much , it worked !!!

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

추가 답변 (0개)

카테고리

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

태그

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by