How to Sum the N newest data

1 回表示 (過去 30 日間)
Hao Zhang
Hao Zhang 2019 年 9 月 9 日
編集済み: KALYAN ACHARJYA 2019 年 9 月 9 日
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!!

回答 (1 件)

KALYAN ACHARJYA
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 件のコメント
Hao Zhang
Hao Zhang 2019 年 9 月 9 日
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 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....

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeApplication Deployment についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by