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일

0 개 추천

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

Thank you for this. Although a nice way of doing this I think the challenge is that the calculation is all done in parallel and therefor doesn't capture how a calculated value is input into the next calculation.
Values for Var3 in the original problem:
B =
9×3 table
Var1 Var2 Var3
____ ____ ________________
1 1 1
1 2 1.43464717050245
1 3 2.52322347665458
1 4 2.14595795245583
1 5 3.63511709803536
2 6 6
2 7 1.6563005366775
2 8 6.99402697270611
3 500 500
When running your suggestion
B =
9×3 table
Var1 Var2 Var3
____ ____ _________________
1 1 1
1 2 1.43464717050245
1 3 0.649519052838329
1 4 1
1 5 1.39754248593737
2 6 6
2 7 1.6563005366775
2 8 2.82842712474619
3 500 500
If one takes a look at B.Var3(3) that is the same as power(sqrt(3)/2,3) while what I wanted was power((sin(B.Var3(2)) + sqrt(B.Var2(3)))/2,3) which was a different number.
Do you see what I mean
KSSV
KSSV 2020년 10월 22일
Edited the answer....check now...
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개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

태그

질문:

2020년 10월 22일

댓글:

2020년 10월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by