MATLAB: Calculation across rows without FOR LOOP

for loopMATLAB

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

Best Answer

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);