I'm trying to display a 2D table

9 views (last 30 days)
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).

Accepted Answer

Benjamin Thompson
Benjamin Thompson on 4 Oct 2022
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.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Tags

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!