build a special matrix using a for loop

1 Ansicht (letzte 30 Tage)
ilona
ilona am 24 Nov. 2013
Beantwortet: ilona am 24 Nov. 2013
I tried to build a (9,9) matrix that will look like that:
% code
first row: 000000009
second row: 000000088
third row : 000000777
forth row : 000006666
fifth row : 000055555
6th row : 000444444
7th row : 003333333
8th row : 022222222
9th row : 111111111
my code is:
% code
mymat=zeros(9,9);
for ii=1:(size(mymat,1))
for jj=1:(size(mymat,2))
mymat(ii,[9(10-jj)])= 10-ii;
end
end
the result is this:
% code
0 0 0 0 0 0 0 0 9
0 0 0 0 0 0 0 0 8
0 0 0 0 0 0 0 0 7
0 0 0 0 0 0 0 0 6
0 0 0 0 0 0 0 0 5
0 0 0 0 0 0 0 0 4
0 0 0 0 0 0 0 0 3
0 0 0 0 0 0 0 0 2
0 0 0 0 0 0 0 0 1
what am I doing wrong?

Akzeptierte Antwort

ilona
ilona am 24 Nov. 2013
OMG! sorry I found out that my index was wrong! the correct answer is:
% code
mymat=zeros(9,9);
for ii = 1:(size(mymat,1))
mymat(ii,[(10-ii):9])= 10-ii;
end

Weitere Antworten (0)

Kategorien

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

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by