Reshape 2D matrix to 3D using time as 3rd dimension

1 view (last 30 days)
Hi,
I've got a A = n x n matrix and I want to convert this 2D matrix to a 3D one using time as the third dimension. So the 3D should show how the value of all the elements in the n x n matrix changes with respect to time.
I tried using the reshape function but to no avail.
Thanks!

Accepted Answer

Thorsten
Thorsten on 28 Oct 2015
If i is your related to your time and takes values 1,2,3, etc, and A is the n x n matrix at time point i, you can use:
B(:,:,i) = A;
  2 Comments
iijoclu
iijoclu on 28 Oct 2015
Edited: iijoclu on 28 Oct 2015
Ok so my time function is basically
[kgrid.t_array, dt] = makeTime(kgrid, medium.sound_speed);
When I try to plug kgrid.t_array in the formula above, I get an error saying that the subscripts should be real positive integers or logical.
Size of my kgrid.t_array is [1 5243].
Thorsten
Thorsten on 28 Oct 2015
Edited: Thorsten on 28 Oct 2015
You cannot use the values of kgrid.t_array, but instead 1:numel(kgrid.t_array). Your code should look something like below, assuming that you have a function that computes your NxN matrix for every timestep:
for i=1:numel(kgrid.t_array)
t = kgrid.t_array(i); % get the ith time value
A = computeNxNmatrix(t, more parameters); %
B(:,:,i) = A;
end

Sign in to comment.

More Answers (1)

Matt J
Matt J on 28 Oct 2015
I tried using the reshape function but to no avail.
Be more persistent. RESHAPE is the appropriate thing to use.
  2 Comments
lmaree
lmaree on 12 Mar 2021
"Beyond the second dimension, the output, B, does not reflect trailing dimensions with a size of 1. For example, reshape(A,3,2,1,1) produces a 3-by-2 matrix." from the reshape documentation.
In my case, this is exactly what I need to multiply a spacial 2D matrix with a time vector. However, reshape doesn't have the functionality it seems...
Matt J
Matt J on 12 Mar 2021
Edited: Matt J on 12 Mar 2021
It is not needed. There is no difference between what you can do with a 3x2 matrix and with a 3x2x1x1 matrix.
A=rand(3,2);
t=rand(1,1,1,4);
A.*t
ans =
ans(:,:,1,1) = 0.2134 0.1069 0.0927 0.5706 0.2873 0.2506 ans(:,:,1,2) = 0.1807 0.0905 0.0785 0.4833 0.2433 0.2123 ans(:,:,1,3) = 0.3407 0.1707 0.1481 0.9111 0.4586 0.4002 ans(:,:,1,4) = 0.0462 0.0232 0.0201 0.1236 0.0622 0.0543

Sign in to comment.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!