Graphics Programs Reference
In-Depth Information
first row of a by taking the columnar sum of the triangular matrix s :
a(1,:) = sum(s)
···
11111
02222
00333
00044
00005
.
= sum
.
. . .
We can generate the second row of a by taking the same columnar sum
but leaving out the first row of s :
a(2,:) = sum(s(2:end,:))
···
02222
00333
00044
00005
.
= sum
.
. . .
In general, then, we can generate the i th row of a by taking the columnar
sum of s leaving out its first i
1 rows: a(i,:) = sum(s(i:end,:)) .
Our final code will consist of putting this statement inside a for loop
(this will be a good use of a for loop—see the first paragraph in this
section). Before we do that, though, we still need to generate the utility
matrix s ; here we can use matrix multiplication. The matrix we want
can be obtained by taking the upper triangular part of the product of a
column vector and a row vector:
11111
···
1
2
3
4
5
.
02222
00333
00044
00005
.
· 11111
···
= triu
. . .
So here we have the final code to generate the a matrix (for N = 200):
N = 200;
s = triu((1:N)'*ones(1,N));
a = zeros(N,N);
for i = 1:N-1
a(i,:) = sum(s(i:end,:));
end
a(N,:) = s(N,:);
Search WWH ::




Custom Search