MATLAB Answers

Calculation across rows without FOR LOOP

7 views (last 30 days)
William Ambrose
William Ambrose on 22 Oct 2020
Commented: KSSV on 22 Oct 2020
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

  0 Comments

Sign in to comment.

Accepted Answer

KSSV
KSSV on 22 Oct 2020
Edited: KSSV on 22 Oct 2020
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 Comments

Show 1 older comment
KSSV
KSSV on 22 Oct 2020
Edited the answer....check now...
William Ambrose
William Ambrose on 22 Oct 2020
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 on 22 Oct 2020
Yes....sounds right...you are right..

Sign in to comment.

More Answers (0)

Tags

Products

Community Treasure Hunt

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

Start Hunting!