Asked by Hao Zhang
on 9 Sep 2019

Hi,

Say I have a code to simulate the time evolution of a very large 3D array E (the electric field), for instantce in finite-difference time-domina simulations. So, in each time step, E will be updated to a new value. Now I want to find the sum of the N (N=5 for example) newest E during each time of my simulation, what is the most efficient, that requires the least ammount of memory, to do this?

So, more intuitively,

at t=t1, E is updated to E1.

at t=t2, E is updated to E2.

at t=t3, E is updated to E3.

at t=t4, E is updated to E4.

at t=t5, E is updated to E5.

.

.

.

at t=tn, E is updated to En.

What I require is the following:

at t=t1, I require S=E1.

at t=t2, I require S=E1+E2.

at t=t3, I require S=E1+E2+E3.

at t=t4, I require S=E1+E2+E3+E4.

at t=t5, I require S=E1+E2+E3+E4+E5.

at t=t6, I require S=E2+E3+E4+E5+E6.

at t=t7, I require S=E3+E4+E5+E6+E7.

at t=t8, I require S=E4+E5+E6+E7+E8.

.

.

.

at t=tn, I require S=E_{n-4}+E_{n-3}+E_{n-2}+E_{n-1}+E_{n}

I do not want to use a new vaiable to keep storing the 5 newest data since E is a very large 3D matrix. Is there any other way to do it?

Many thanks!!

Answer by KALYAN ACHARJYA
on 9 Sep 2019

Edited by KALYAN ACHARJYA
on 9 Sep 2019

One way: % Assuming E1,E2,E3...are scalars

n= %total_data t1,t2,t3........

s=0;

E=zeros(1,);

for t=1:n % t time represents in your case

E(t)= ....% Updated

S=sum(E(1:t))

end

Any issue let me know?

KALYAN ACHARJYA
on 9 Sep 2019

If so??

%First calculate E(1) to E(5) then

for t=5:n

E(t)=sum(E(t-4:t)) ....% Updated

.....

end

E1,E2, are scalars or vectors?

Hao Zhang
on 9 Sep 2019

E1 E2... are also very large 3D matrix. Even the 5 latest value of E I do not want to store them in memory. Because the 5 is just a number I use for this example. It can be much larger in the real calculation.

Any way to only store the result of summation without explicitly storing the 5 latest E?

KALYAN ACHARJYA
on 9 Sep 2019

E1 E2... are also very large 3D matrix,, If you want to latest 5 also..

Then you have to use (save) E{1}, E{2} as cell array elements and call the respective to compute the required

Save the last 5 iterartion E in cell array and call the cell elemets to sum the data, after 6 iteration replace the first cell array elements ...so ..on....

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 0 Comments

Sign in to comment.