简体   繁体   English

Matlab中有一个function来创建矩阵,其中每个元素是相同的矩阵索引的function?

[英]There is a function in Matlab to create a matrix, where each element is the same function of matrix indexes?

For example, a matrix where each value at row r and column c is例如,一个矩阵,其中行 r 和列 c 的每个值是

a=[r^2+c^2]

like a=[1, 4; 4; 18]比如a=[1, 4; 4; 18] a=[1, 4; 4; 18]

or或者

A=[F(r,c)]

A=[F(1,1) F(1,2) F(1,3);
   F(2,1) F(2,2) F(2,3);
   F(3,1) F(3,2) F(3,3)]

or或者

A(r,c)=F(r,c)

Mehtod 1方法一

You can do it manually.您可以手动完成。 First, create two matrices for rows and columns indices (suppose the matrix is nxm ):首先,为行和列索引创建两个矩阵(假设矩阵是nxm ):

R = repmat((1:n).',[1, m]); 
C = repmat((1:m),[n, 1]);

Then, write the function base on these two:然后,在这两个基础上编写function:

result = R.^2 + C.^2; % F(x,y) = x^2 + y^2

Or define the function inline and apply it on those two:或者定义 function 内联并将其应用于这两个:

F = @(x,y)(x.^2 + y.^2);
result = F(R,C);

Mehtod 2方法二

By @Cris Luengo, you can do it the first part by the meshgird function as well.通过@Cris Luengo,您也可以通过meshgird function 来完成第一部分。 Hence, we can generate R and C like the following:因此,我们可以生成RC ,如下所示:

[C,R] = meshgrid(1:n, 1:m) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM