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 for Programming
 LabTalk Forum
 Averaging X cells of different columns on a sheet
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

onoseque

Germany
8 Posts

Posted - 12/28/2020 :  03:57:51 AM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
Origin Ver. 2021 Student Version
Operating System: Windows

Hi and sorry if I'm asking too basic stuff but I am just starting with coding.
I have a sheet resulting of unstacking batch peak analyzing. Every 8 columns corresponds to 1 data set (total number of columns in the sheet 184).
I want to average every 5 cells of the second column (of every 8 column serie). So I use

wks.addCol(Result);
col(Result)=ave(col(b),5);
col(Result)[L]$ = Peak1;
col(Result)[U]$ = Area;

And this works for the first column. My problems:
1) I am reading about the looping over columns but don't succeed in making it repeat the same operation on the second column of every 8th.
2) Making manual tests in changing the column the script rewrites the results in the same "Results" Column. So it creates a new "Result1" col but writes the data into the the previous "Result" column. I am aware that this must be a very basic issue to create a new result column on every run. The long name should of course also change (Peak 1, 2 etc etc)
3) I need to do the exact same operation but obtaining the standard deviation instead of the average. I have tried
col(Error)=double StdDev(col(b),5);
but it doesn't work. Any tip?

Thank you so much

cpyang

USA
1406 Posts

Posted - 12/28/2020 :  11:13:30 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
You are using ave function? How about the X value? Is using moving average ok? Using moving average will allow the original X column to be used. I assume you will need to plot the result and thus the correct X values will be needed?

CP
Go to Top of Page

onoseque

Germany
8 Posts

Posted - 12/29/2020 :  03:27:20 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi! thanks for your message. Yes, I was going for the ave function. It is not the moving average what I want, but the average of fixed groups (so row 1-5, row 6-10 etc) and the sample standard deviation of the same groups. Then repeat the process for every 2nd of every 9 columns
Go to Top of Page

YimingChen

1665 Posts

Posted - 12/29/2020 :  12:18:17 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi,

1. You can refer to the newly added column by index, so the index of last column is the wks.ncols, which is the counts of columns.
2. We don't have group stddev function and will add it. For now you may calculate the group stddev with formula sqrt((E(X^2) - E(X)^2))

Please try the script below:

wks.addCol();
wcol(wks.ncols)[L]$ = Peak1;  // wks.ncols gives the index of last column
wcol(wks.ncols)[U]$ = Area;
wcol(wks.ncols)[O]$= "ave(B,5)";

wks.addCol();
wcol(wks.ncols)[L]$ = Peak1;
wcol(wks.ncols)[U]$ = Error;
wcol(wks.ncols)[O]$= "sqrt((ave(B^2,5) - ave(B,5)^2))"; // std = sqrt((E(X^2) - E(X)^2))


James

Edited by - YimingChen on 12/29/2020 2:09:55 PM
Go to Top of Page

YimingChen

1665 Posts

Posted - 12/29/2020 :  1:07:38 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi,

In Origin 2021, you can also use Python script for such. Select from menu Connectivity:Open Untitiled.py..., put in the following script in the Code Builder window, and hit F5 to run.

import numpy as np
import originpro as op
 
wks = op.find_sheet()
nn = wks.cols

wks1 = op.new_sheet()
wks1.cols = 0

for i, idx in enumerate(range(1, nn, 9)):  # loop over second col of every 9 columns
    data = np.asarray(wks.to_list(idx))
    nc = len(data) // 5
    data = np.split(data[:5*nc],nc)
    wks1.from_list(wks1.cols, np.mean(data,1), f"Peak{i+1}", 'Area')
    wks1.from_list(wks1.cols, np.std(data,1), f"Peak{i+1}", 'Error')


James

Edited by - YimingChen on 12/29/2020 4:54:28 PM
Go to Top of Page

onoseque

Germany
8 Posts

Posted - 12/30/2020 :  02:17:25 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi James, thanks! It worked perfect. So I guess at this point, it doesn't make sense to learn LabTalk any more?
Thanks again and cheers
Go to Top of Page

cpyang

USA
1406 Posts

Posted - 12/30/2020 :  05:18:21 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Yes anything that is actually programming in Origin it is better to go with Python, as we will continue to improve Python support with the originpro package.

If you are already familiar with programming in Labtalk, make sense to continue, but otherwise, then Python is the way going forward.

CP
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