# How to create a 3D matrix out of 2D matrices?

26 views (last 30 days)
Moha al on 27 May 2019
Commented: Guillaume on 12 Jun 2019
Hi all and thank you for reading my question
I'm trying to calculate velocities of particles of water away from a source of energy.
I don’t have very good skills in Matlab so you may be able to help me running the code faster.
I used to do it like that :
For x=1:15;
For y=-4000;4000;
t0=(sqrt(y0.^2+x0.^2) ./ c);
end
end
for i=1:4000000
A=y*t+s*x;
B=sqrt(x+y)*1./x;
And many other calcualtions to get
U(x,y,t)= ;
Store U value in a vector
Store t0 value in a vector
T0=t0+0.001;
end
M=max(U);
store x value in a vector
store y value in a vector
store t value in a vector
end
end
But thanks to Walter Robertson I have done the following :
c=1000;
x=[1:15];
y=[-4000:4000];
[x0,y0]=ndgrid[x,y];
t0=(sqrt(y0.^2+x0.^2) ./ c);
size of x0,y0 and t0 = 15x8001
now I want to create an array of 4000000 components for each value of t0 that increases by 0.001 such that
t_(n)=t_(n-1)+0.001
What I have in my mind is to have it as 3D matrix such that each matrix = previous matrix+0.001
for 4000000 times.
I hope I've cleared the question and you'd be able to help.
Thanks

Raghunandan V on 28 May 2019
You calcualte t0 from x0 and y0. And at the end you tell you have find x0 and y0 for each value of t0. Its not possible right?
Moha al on 28 May 2019
Hi raghunandan
Thank you for reading my question.
Basically I calculate t0 using each value of x0 and y0. No I need to create an array of each t0 increasing by 0.001 as a time series. So I have t0 for each x0,y0, And I want to have an array [t0, t0+0.001, ...] for each x0,y0
Hope it's clearer now

Nada Kamona on 29 May 2019
Hi Moha,
I'm not 100% sure on what you're looking for, but based on my understanding you have a matrix t0 of size 15x8000. Then you want to increment every element in t0 by 0.001 several times. The results you want to store in a 3D matrix t, such that the first layer is t0 and the other layers are incremented by 0.001. So you have [t0, t0+0.001, t0 + 0.002, ... ]. Is this correct?
If the above is what you want, then you can write your code like this:
numIterations = 10; % the number of times you want to increment by 0.001
t = zeros(size(t0,1), size(t0,2),numIterations); % initialize your 3D variable t
for i = 1:numIterations
t(:,:,i) = t0 + (i-1)*0.001; % increment by 0.01, such that t = previous t + 0.001
end
Perhaps you can even make it faster than that. But these were my initiale thoughts. Hope this helps!

Moha al on 12 Jun 2019
thank you so much for your help.
Rik's answer works well. However, I'm still unable to do it for the number of iterations desired (4000000) as it gives me an error when trying it for high number of iterations :
''Requested 15x4001x1000001 (447.1GB) array exceeds maximum array size preference. Creation of arrays greater than this limit may take a long time and cause MATLAB to become unresponsive. See array size limit or preference panel for more information.''
please execuse me for all of these questions but it's my first time to work with Matlab and it gives me lot of pain.
Thanks
Rik on 12 Jun 2019
There is a fundamental limit in how large of an array you can store in your computer's memory. What you want is not currently possible. You might be able to reduce your array size by about a factor 8 by switching to a data type that stores less information, but that will still require 56 GB of contiguous memory.
You need to change your strategy so you don't need to store everything all at once.
Guillaume on 12 Jun 2019
@Moha,
The error message you get is very clear. You want to create so many values that it needs over 400 GB of memory. Computer typically only have 4 to 16 GB of memory so you're way over what could possibly be stored in your computer memory. Even if you had enough memory to store all these numbers, it would be very slow to process as once again you want to have so many values.
Note that this has nothing to do with matlab. You need to significantly reduce your problem size or completely rethink what you're doing.

Raghunandan V on 29 May 2019
Hi,
The t0 you get from :
t0=(sqrt(y0.^2+x0.^2) ./ c);
t0 here is a matrix. So when you tell you want to increase t0 by 0.001 then you are thinking of adding the whole t0 matrix by 0.001. So this is how you do it:
[q, p]= size(t0);
t0_matrix_new = t0 + 0.001*ones(q,p)
since x0 and y0 are always constant you can used them directly where ever you want. You dont need to have a 3d matrix

Moha al on 1 Jun 2019
Hi Raghunandan
Thank you for your time and help.
x0 and y0 are constant for each t0 and not for the whole matrix t0.
each x0 and y0 are used to calculate t0.
then I want to take each t0 as an array that increases 0.001 for each element
then I'll have an array t0 for each x0 and y0
Thank you
Raghunandan V on 3 Jun 2019
Sorry Moha al,
I still am not able to understand the question.
If possible try to post a sample question with answert
Moha al on 12 Jun 2019
Hi
I'm trying to calculate drifting of a water particle along x and y.
so for each point(x,y) away from a source of disturbence there is a critical time when the pressure signal arrives.
what I want to see here is what happens in every point (x,y) from the origin during certain time (i.e 4000 sec) where the initial time is the critical time then I take time step 0.001
so for example
for x=100000, y=0 , t=(sqrt(0^2+1000^2) / 1500)=66.667
now for x=100000,y=0 I need to have an array (1x4000000) of time starts with 66.667 and increases by 0.001
note that x=[1:15] (km)
and y=[-4000:4000] (m)
hope this helps
Thanks for your time and help