15 views (last 30 days)

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

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!

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.

Sign in to comment.

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

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

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/464232-how-to-create-a-3d-matrix-out-of-2d-matrices#comment_709236

⋮## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/464232-how-to-create-a-3d-matrix-out-of-2d-matrices#comment_709236

## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/464232-how-to-create-a-3d-matrix-out-of-2d-matrices#comment_709516

⋮## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/464232-how-to-create-a-3d-matrix-out-of-2d-matrices#comment_709516

Sign in to comment.