简体   繁体   中英

Matlab. Storing 2D arrays within 3D arrays

I have defined the following 2D function,

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,

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?

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.

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

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

Or bsxfun

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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