How do i write a script that will print the following multiplication table 1; 2 4; 3 6 9; 4 8 12 16; 5 10 15 20 25

17 Ansichten (letzte 30 Tage)
How do i write a script that will print the following multiplication table 1;2 4;3 6 9; 4 8 12 16; 5 10 15 20 25

Antworten (4)

asma sarouji
asma sarouji am 2 Nov. 2018

Guillaume
Guillaume am 30 Apr. 2015
This looks like homework (and really easy at that), so here is a very simple solution that probably wouldn't be accepted for homework:
arrayfun(@(x) (1:x)*x, 1:5, 'UniformOutput', false); celldisp(ans)

PaulS
PaulS am 30 Apr. 2015
Bearbeitet: Walter Roberson am 20 Aug. 2021
n=5
A=zeros(n)
for i=1:n
for j =1:n
if j<=i
A(i,j)=i*j
end
end
end
  2 Kommentare
Scott
Scott am 30 Apr. 2015
Awesome! How do you display this answer so that it prints it in a matrix form?
Aitazaz Ahsan
Aitazaz Ahsan am 20 Aug. 2021
There is already a matrix with all entries equal to zero so in for loop only the concerned entries are changing and which will give the desired solution inmatrix form.

Melden Sie sich an, um zu kommentieren.


Tony Nguyen
Tony Nguyen am 15 Aug. 2021
clc;
clear all;
for i = 1:5
fprintf('%4d',i*[1:i])
fprintf('\n')
end

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by