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
 Forum for Origin C
 Average Multiple Columns with an input parameter
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

kawonoum

Germany
Posts

Posted - 03/09/2008 :  9:09:56 PM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
Origin Version (Select Help-->About Origin): 7.5
Operating System: XP
Hi,

I have a question about "Average Multiple Columns".

I have a worksheet containing one X-column and 512 Y-columns.
I would like to group the Y-columns every, e.g., 16 columns and average them, then create a new worksheet contining X-column and the averaged columns (in this case 512 / 16 = 32 columns).
I need to use an arbitrary number (16 as above) as an input parameter to group the columns.

I am new with Origin C. Could anyone help me how to program in this case?

Many thanks in advance,

Kawon

Iris_Bai

China
Posts

Posted - 03/13/2008 :  05:06:57 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi Kawon,

Please try the following codes, hope help you. I tried in 7.5Sr7, it works.


void average_columns(int nNumColsOfOneGroup = 16)
{
// get source data worksheet containing one X-column and 512 Y-columns
Worksheet wks = Project.ActiveLayer();
if(!wks || 513 != wks.GetNumCols())
{
out_str("No active worksheet or no specified worksheet including one X column and 512 Y columns!");
return;
}

//prepare output worksheet
Worksheet wksOut;
wksOut.Create("origin");

// Copy X data from source worksheet to reuslt worksheet
Dataset dsX(wks, 0);
Dataset dsXNew(wksOut, 0);
dsXNew = dsX;

// average Y columns for each 16 columns and put result to new worksheet
int nBeginCol = 1;
int nEndCol = nBeginCol + nNumColsOfOneGroup - 1;
int nResultCol = 1; //the first column is X, so the first Y column beginning from 1
for(int nNumGroups = 0; ;nNumGroups++)
{
// get data and do average
Column colBegin(wks, nBeginCol);
if(!colBegin)
break;

vector& vData1 = colBegin.GetDataObject();
for(int nCol = nBeginCol+1; nCol <= nEndCol; nCol++)
{
Column col(wks, nCol);
if(!col)
break;

vector& vData2 = col.GetDataObject();
vData1 = vData1 + vData2;
}
vData1 = vData1 / nNumColsOfOneGroup;

// put reuslt into wksOut sheet
Column colResult(wksOut, nResultCol);
if(!colResult)
colResult.Attach(wksOut, wksOut.AddCol());
Dataset dsResult(colResult);
dsResult = vData1;
nResultCol++;

// continue to average next group
nBeginCol += nNumColsOfOneGroup;
nEndCol += nNumColsOfOneGroup;
}
}



Iris
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