Calculation across rows without FOR LOOP

12 次查看(过去 30 天)
Hi,
I have a table with 20 million records where I need to calculate a value which is dependent on a previous row's value and the current row's value. This new value is subsequently input to the next row. I have solved this with a FOR LOOP but with 20 million records this is very slow. I have previously got suggestions for use of cumprod and vectorized calculations which have been very useful, but due to a simplification of the original problem didn't really work. I have therefore redefind the issue and was hoping someone would have another potential solution without the FOR LOOP.
The calculation below is not a real issue, it is intended to show that I have a problem which needs to be solved sequentially with one value having been calculated is subsequently used as input into the next calculation.
Thank you for your time and help.
Kind regards,
William
B = table([1;1;1;1;1;2;2;2;3],[1;2;3;4;5;6;7;8;500]);
B.Var3 = zeros(height(B),1);
i = [false; B.Var1(1:end-1) == B.Var1(2:end)];
j = find(~i);
B.Var3(j) = B.Var2(j);
for n = 1:height(B)
if i(n) == 1
B.Var3(n) = power((sin(B.Var3(n-1)) + sqrt(B.Var2(n))) / 2,3);
end
end
>> B
B =
9×3 table
Var1 Var2 Var3
____ ____ ______
1 1 1
1 2 1.4346
1 3 2.5232
1 4 2.146
1 5 3.6351
2 6 6
2 7 1.6563
2 8 6.994
3 500 500

采纳的回答

KSSV
KSSV 2020-10-22
编辑:KSSV 2020-10-22
B = table([1;1;1;1;1;2;2;2;3],[1;2;3;4;5;6;7;8;500]);
B.Var3 = zeros(height(B),1);
i = [false; B.Var1(1:end-1) == B.Var1(2:end)];
j = find(~i);
B.Var3(j) = B.Var2(j);
idx = find(i == 1) ;
T = sin(B.Var3) ;
B.Var3(idx) = power((T(idx-1) + sqrt(B.Var2(idx))) / 2,3);
  4 个评论
William Ambrose
William Ambrose 2020-10-22
Thank you, but I still get the same answer, the issue is that I need to calculate a number and then in the next instance use the newly calculated number as input to the next calculation. Hence I see a need for sequential calculations whilst I think your approach doesn't do this, unless I am missing something.
KSSV
KSSV 2020-10-22
Yes....sounds right...you are right..

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by