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

조회 수: 10(최근 30일)
Moha al 2019년 5월 27일
댓글: Guillaume 2019년 6월 12일
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
##### 댓글 수: 2표시숨기기 이전 댓글 수: 1
Moha al 2019년 5월 28일
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

댓글을 달려면 로그인하십시오.

### 답변(2개)

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!
##### 댓글 수: 6표시숨기기 이전 댓글 수: 5
Guillaume 2019년 6월 12일
@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 2019년 5월 29일
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
##### 댓글 수: 3표시숨기기 이전 댓글 수: 2
Moha al 2019년 6월 12일
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

댓글을 달려면 로그인하십시오.

### Community Treasure Hunt

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

Start Hunting!