The Origin Forum
File Exchange
Try Origin for Free
The Origin Forum
Home | Profile | Register | Active Topics | Members | Search | FAQ | Send File to Tech support
Username:
Password:
Save Password
Forgot your Password? | Admin Options

 All Forums
 Origin Forum
 Origin Forum
 moving sum
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

manueljiza

2 Posts

Posted - 04/12/2023 :  7:17:56 PM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
Origin Ver. and Service Release (Select Help-->About Origin): OriginPro 2023 (Learning Edition)
Operating System: Windows 10 Pro Build 19043

Hi, I want to know if there is a way to perform a moving sum of 'n' cells of a column, with 'n' being a value defined in a cell from other column.

For example, if cell B1 = 10, sum only the values from A1 to A10. And, if cell B2 = 11, sum only the values from A2 to A11.

The formulas could look like this:

C1=total(A1:A[B1])
C2=total(A2:A[B2])
...
C[i]=total(A[i]:A[B[i]])


but it seems that indexes doesn't work in functions like sum, total, stddev...

Thanks in advance for the attention!

aplotnikov

Germany
169 Posts

Posted - 04/13/2023 :  06:19:08 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
int iR;
loop(iR, 1, wks.maxRows) {
	//colB[iR]>=iR !!!!
	range rgR = col(A)[iR:$(col(B)[iR])];
	col(C)[iR] = rgR.GetSize()>1 ? total(rgR) : rgR[1];
}
Go to Top of Page

snowli

USA
1429 Posts

Posted - 04/13/2023 :  11:37:11 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
You can define a name for B1 cell and use the defined name in Column Formula.


Note: B1 should be 9 to get from A1 to A10

1. Single click on B1. A mini toolbar shows. Click the Define Name button.



2. Define a name for B1 cell, e.g. count


3. Then enter the F(x) of C to be total(A[i:i+count])
Note: The size of C will be determined by the left column of it so u will need to go to Set Column Values to set the To value to the right row number.


named range can be used in formula, fitting, etc. Read more on https://www.originlab.com/doc/en/Origin-Help/Named-Range

Thanks, Snow
Go to Top of Page

manueljiza

2 Posts

Posted - 04/13/2023 :  12:45:15 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Thank you so much aplotnikov and snowli! The method of snowli works and is pretty straightforward. The code of aplotnikov is more difficult for me to apply but I'm delving into it for being able to do more complex stuff. Thanks again guys!
Go to Top of Page

aplotnikov

Germany
169 Posts

Posted - 04/13/2023 :  12:59:45 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
1. Make the workbook window active.
2. Press Alt-3 to show the Command Line.
3. Copy the provided code and paste it to the Command Line, press Enter.
Go to Top of Page
  Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
The Origin Forum © 2020 Originlab Corporation Go To Top Of Page
Snitz Forums 2000