简体   繁体   English

Matlab。 在3D阵列中存储2D阵列

[英]Matlab. Storing 2D arrays within 3D arrays

I have defined the following 2D function, 我定义了以下2D函数,

Ngrid = 100;
h     = 1/(Ngrid-1);
x     = 0:h:1;
y     = 0:h:1;
[x y] = meshgrid(x,y);
f = exp(-((1-x).^2)./0.45)

and I want to store this function within the 3D array "c",along the "T" dimension, 我想将这个函数存储在3D数组“ c”中,以及“ T”维度中,

k    = 0.001;
Tend = 1; 
T    = 0:k:Tend;
c    = zeros(length(T),length(x),length(y));

What I have tried is, 我试过的是

c(1:end,:,:) = f;

but it does not work. 但它不起作用。 ¿Any idea of how can I store the same function within this 3D array? ¿关于如何在3D阵列中存储相同功能的任何想法?

Thanks in advance. 提前致谢。

The subscript dimension mismatch is because you are trying to squeeze 100 * 100 elements into a 1001 x 100 x 100 matrix. 下标尺寸不匹配是因为您试图将100 * 100元素压缩到1001 x 100 x 100矩阵中。

You could do this assignment the following way: 您可以通过以下方式进行此分配:

c(1,:,:) = f;
c(2,:,:) = f;
...
c(1001,:,:) = f;

But you can accomplish the same thing using repmat 但是您可以使用repmat完成相同的操作

c = repmat(reshape(f, [1, size(f)]), [numel(T), 1 1]);

Or bsxfun bsxfun

c = bsxfun(@plus, zeros(numel(T), 1), reshape(f, [1, size(f)]));

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

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