2015-02-11 98 views
1

我对Matlab很陌生。我想通过M_ {ij} = f(i,j)来指定M×N矩阵。在Mathematica中,我会写Table[f[i,j], {i,1,m}, {j,1,n}]甚至更​​简单Array[f,{m,n}]。在Matlab中做到这一点最简单的方法是什么?Matlab等价于Mathematica的表或数组

+1

看一看ndgrid/meshgrid – Trilarion 2015-02-11 12:52:28

回答

3

我不熟悉的数学语法,但我

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)]