Show older comments
How to create a matrix M1 in which each entry is the sum of the row index number and the column index number???
second question
How to create a matrix X that is equal to the matrix product of a and c ( which are previously created) =[1 1 1;0 1 1;0 0 1]
Answers (2)
Matt Fig
on 21 Jan 2011
% Create an m-by-n matrix where each element is sum of row and col position.
D = ones(m,n);
[I,J] = find(D);
D(:) = I+J
Another way too:
D = ones(m,n);
D(:) = ceil((1:m*n)/m) + rem((1:m*n)-1,m) + 1
Your second question is not clear. Elaborate.
Walter Roberson
on 21 Jan 2011
I am not sure what the matrices "a" and "c" represent, but matrix multiplication between the two would be X = a*c .
For the matrix M1:
m = 5; n = 6; mn = max(m,n);
t = hankel(2:mn+1) + fliplr(flipud(hankel([2*mn:-1:mn+2 0])));
M1 = t(1:m,1:n);
... Not that Matt's answer won't work fine, but sometimes it is nice to know about alternatives.
But the cleaner version of what Matt proposed is:
M1 = bsxfun(@plus,(1:m).',1:n);
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!