How to Sum the N newest data
조회 수: 1 (최근 30일)
이전 댓글 표시
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!!
댓글 수: 0
답변 (1개)
KALYAN ACHARJYA
2019년 9월 9일
편집: KALYAN ACHARJYA
2019년 9월 9일
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
KALYAN ACHARJYA
2019년 9월 9일
편집: KALYAN ACHARJYA
2019년 9월 9일
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....
참고 항목
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!