MATLAB: Multiplying the row numbers and column numbers, NOT THE ELEMENTS iN THEM

column-orderMATLABmatrix manipulationmultiplicationrow-order

So, i have been coming up across this function a lot in the last week or so and i haven't been able to create a succinct way of doing this calculation. What i am trying to do is have a function that multiplies the row and column numbers, NOT the elements in those rows and columns, but the row number and column number. So, for matrix coordinates (1,2), the result that should be displayed at that location should be 2 based on their multiplication. Likewise, for (8,3), it should be 24 based on the multiplication of the row and column number. Is there a way that i could do that??

Best Answer

Assuming e.g.:
nRows = 5 ;
nCols = 8 ;
you can do it this way:
prods = bsxfun( @mtimes, (1:nRows)', 1:nCols ) ;
which creates
prods =
1 2 3 4 5 6 7 8
2 4 6 8 10 12 14 16
3 6 9 12 15 18 21 24
4 8 12 16 20 24 28 32
5 10 15 20 25 30 35 40
EDIT: There are multiple way of doing it though; another would be:
prods = repmat( (1:nRows)', 1, nCols ) .* repmat( 1:nCols, nRows, 1 ) ;