2015-02-11 99 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)]