How to Sum the N newest data

1 Ansicht (letzte 30 Tage)
Hao Zhang
Hao Zhang am 9 Sep. 2019
Bearbeitet: KALYAN ACHARJYA am 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!!

Antworten (1)

KALYAN ACHARJYA
KALYAN ACHARJYA am 9 Sep. 2019
Bearbeitet: KALYAN ACHARJYA am 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?
  6 Kommentare
Hao Zhang
Hao Zhang am 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
KALYAN ACHARJYA am 9 Sep. 2019
Bearbeitet: KALYAN ACHARJYA am 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....

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu MATLAB finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by