Is there a way to generate a matrix where each element is defined as 10 + row_index + column_index without loops?

I am trying to create a matrix where each element is defined as 10 * row_index + column_index. Rows and columns can be resized up to a 9x9 matrix. For example:

11    12    13    14    15    16
21    22    23    24    25    26
31    32    33    34    35    36
41    42    43    44    45    46
51    52    53    54    55    56

      

The algorithm is extremely simple with loops for

, but I have been warned to avoid loops for

unless absolutely necessary when working with matrices because they are slower than vector / matrix operations.

What other ways to create such a matrix in Matlab 2012b?

+3


source to share


1 answer


For your specific matrix, it's pretty straightforward:



nRows = 4;
nCols = 5;

out = bsxfun(@plus,10*(1:nRows)',1:nCols)

out =

11    12    13    14    15
21    22    23    24    25
31    32    33    34    35
41    42    43    44    45

      

+6


source







All Articles