How to create a 3D matrix out of 2D matrices?
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Raghunandan V
il 28 Mag 2019
Hi, i am confused wth question. please help me here.
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?
Risposte (2)
Nada Kamona
il 29 Mag 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!
6 Commenti
Rik
il 12 Giu 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
il 12 Giu 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
il 29 Mag 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
3 Commenti
Raghunandan V
il 3 Giu 2019
Sorry Moha al,
I still am not able to understand the question.
If possible try to post a sample question with answert
Vedere anche
Categorie
Scopri di più su Logical in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!