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
 All Forums
 Origin Forum for Programming
 Forum for Origin C
 Average Multiple Columns with an input parameter

Note: You must be registered in order to post a reply.
To register, click here. Registration is FREE!

Screensize:
UserName:
Password:
Anti-Spam Code:
Format Mode:
Format: BoldItalicizedUnderlineStrikethrough Align LeftCenteredAlign Right Horizontal Rule Insert HyperlinkUpload FileInsert Image Insert CodeInsert QuoteInsert List
   
Message:

* HTML is OFF
* Forum Code is ON
Smilies
Smile [:)] Big Smile [:D] Cool [8D] Blush [:I]
Tongue [:P] Evil [):] Wink [;)] Clown [:o)]
Black Eye [B)] Eight Ball [8] Frown [:(] Shy [8)]
Shocked [:0] Angry [:(!] Dead [xx(] Sleepy [|)]
Kisses [:X] Approve [^] Disapprove [V] Question [?]

 
Check here to subscribe to this topic.
   

T O P I C    R E V I E W
kawonoum Posted - 03/09/2008 : 9:09:56 PM
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
1   L A T E S T    R E P L I E S    (Newest First)
Iris_Bai Posted - 03/13/2008 : 05:06:57 AM
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

The Origin Forum © 2020 Originlab Corporation Go To Top Of Page
Snitz Forums 2000