Clear Filters
Clear Filters

build a special matrix using a for loop

1 view (last 30 days)
ilona
ilona on 24 Nov 2013
Answered: ilona on 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?

Accepted Answer

ilona
ilona on 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

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!