Author |
Topic |
|
Force
Denmark
9 Posts |
Posted - 08/26/2014 : 07:56:20 AM
|
Origin Ver. and Service Release (Select Help-->About Origin): origin 9.1pro Operating System: win7
Hi, I am still relative new with Origin LabTalk but I have a question that I hope someone can help me with. I am looking for at way to reduce my data by grouping them in intervals/bins (x-values) and calculate the sum of the corresponding y-values within each bin. I have tried:
wsort bycol:=10 c1:=10 c2:=14; // Sort col10 to col14 reducexy [%A]%B!(col(10),col(11)) subgroup:=inc xincr:=10 xstats:=last ystats:=ave oy:=(<autoX>,<new>); However, I need ystats:=ave to be ystats:=sum, but apparently the sum it is not supported by this function. How do I solve this problem?
Best regards, Franz
|
|
greg
USA
1378 Posts |
Posted - 08/27/2014 : 12:56:17 PM
|
This will be supported in Origin 2015 - scheduled for release later this year. Until then, you can use this script (after your sort): // BEGIN SCRIPT // Setup ranges range ra1 = 10, ra2 = 11; // your X,Y columns wks.addcol(LastX); // To receive groupX range ra3 = $(wks.ncols); wks.addcol(SumY); // To receive groupY range ra4 = $(wks.ncols); // Initialize xincr = 10; xstart = ra1[1]; rowstart = 1; row = 1; // Now loop loop(ii,2,ra1.GetSize()) { xval = ra1[ii]; if(xval >= xstart + xincr) { // Summarize stats ra2[rowstart:ii-1]; ra3[row] = ra1[ii-1]; // Last X, not the same as // ra3[row] = xstart + xincr; // Last possible X ra4[row] = stats.sum; row++; xstart = xstart + xincr; rowstart = ii; } } // Final group stats ra2[rowstart:ii-1]; ra3[row] = ra1[ii-1]; // ra3[row] = xstart + xincr; ra4[row] = stats.sum; //END SCRIPT
|
|
|
Force
Denmark
9 Posts |
Posted - 08/28/2014 : 05:23:10 AM
|
Hi Greg
Your script works perfect. Thanks. However, if I include an extra Y column I end up with two new identical Y columns. Do I define my range wrong?
wsort bycol:=7 c1:=7 c2:=9;
range ra1 = 7, ra2 = 8, ra3 = 9; // your X,Y columns wks.addcol(10); // To receive groupX; range ra4 = $(wks.ncols); wks.addcol(11); // To receive groupY range ra5 = $(wks.ncols); wks.addcol(12); // To receive groupY range ra6 = $(wks.ncols); // Initialize xincr = 10; xstart = ra1[1]; rowstart = 1; row = 1;
loop(ii,2,ra1.GetSize()) { xval = ra1[ii]; if(xval>= xstart + xincr) { // Summarize stats ra2[rowstart:ii-1]; stats ra3[rowstart:ii-1]; ra4[row] = xstart + xincr; ra5[row] = stats.sum; ra6[row] = stats.sum; row++; xstart = xstart + xincr; rowstart = ii;
}}
// Final group stats ra2[rowstart:ii-1]; ra4[row] = ra1[ii-1]; ra5[row] = stats.sum;
stats ra3[rowstart:ii-1]; ra4[row] = ra1[ii-1]; ra6[row] = stats.sum; }
|
|
|
greg
USA
1378 Posts |
Posted - 08/28/2014 : 09:55:55 AM
|
The stats tree is re-populated after each use of the stats X-Function. If you are doing stats on multiple datasets, you have to use your stats.property before your next use of stats.
Re-arrange some code:
wsort bycol:=7 c1:=7 c2:=9;
range ra1 = 7, ra2 = 8, ra3 = 9; // your X,Y columns wks.addcol(10); // To receive groupX; range ra4 = $(wks.ncols); wks.addcol(11); // To receive groupY range ra5 = $(wks.ncols); wks.addcol(12); // To receive groupY range ra6 = $(wks.ncols); // Initialize xincr = 10; xstart = ra1[1]; rowstart = 1; row = 1;
loop(ii,2,ra1.GetSize()) { xval = ra1[ii]; if(xval>= xstart + xincr) { // Summarize ra4[row] = xstart + xincr; stats ra2[rowstart:ii-1]; ra5[row] = stats.sum; stats ra3[rowstart:ii-1]; ra6[row] = stats.sum; row++; xstart = xstart + xincr; rowstart = ii; } } // Final group ra4[row] = ra1[ii-1]; stats ra2[rowstart:ii-1]; ra5[row] = stats.sum; stats ra3[rowstart:ii-1]; ra6[row] = stats.sum;
|
|
|
Force
Denmark
9 Posts |
Posted - 08/28/2014 : 4:00:33 PM
|
Thanks Greg,
It now works perfectly. |
|
|
|
Topic |
|
|
|