Matlab相当于Mathematica的表或数组

时间:2015-02-11 12:43:19

标签: matlab matrix

我对Matlab很新。我想用M_ {ij} = f(i,j)指定M×N矩阵。在Mathematica中,我会写Table[f[i,j], {i,1,m}, {j,1,n}]或更简单Array[f,{m,n}]。在Matlab中最简单的方法是什么?

1 个答案:

答案 0 :(得分:3)

我不熟悉Mathematica语法,但我对

的理解不熟悉
  

M_ {ij} = f(i,j)

将是

%// I'm making up a function f(x,y), this could be anonymous as in my example or a function in an .m file
f = @(x,y) sqrt(x.^2 + y.^2);  %// note it's important that this function works on matrices (i.e. is vectorized) hence the use of .^ instead of ^

%// make i and j indexing data. Remember Matlab is numerical in nature, I suggest you inspect the contents of X and Y to get a feel for how to work in Matlab...
m = 2;
n = 3;
[X,Y] = ndgrid(1:m, 1:n);

现在它只是:

M = f(X,Y)

结果

M = 
    1.4142    2.2361    3.1623
    2.2361    2.2824    3.6056

即。

 M = [f(1,1), f(1,2), f(1,3);
      f(2,1), f(2,2), f(2,3)]