Search code examples
matlabvectorizationwolfram-mathematica

Equivalent of Mathematica Table in Matlab?


How to vectorize the following code in Matlab?

m = meshgrid(1:n);
for i = 1:n
    for j = 1:n
        m(i,j) = max(i,j);
    end
end

Another way to think of the question would be: how to implement the Mathematica command:

Table[Max[i,j],{i,1,n},{j,1,n}]:

in Matlab.


Solution

  • With implicit expansion,

    m = max([1:n].',[1:n]);
    

    For n = 5:

    m =
    
       1   2   3   4   5
       2   2   3   4   5
       3   3   3   4   5
       4   4   4   4   5
       5   5   5   5   5