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
 weight factor for histograms
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

Michael Sommer

GSF, 85364 Neuherber
4 Posts

Posted - 08/09/2003 :  04:08:36 AM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
Hi there,

I would like to calculate histograms with a "weight factor" for each value, which means the counts of identical values should be included in the calculation procedure for a histogram / probability distribution.

Counts are available in an additional column in the worksheet.
Example:

value counts
2 5
3 10
4 4
5 125

Anybody who can help me ?

Michael


greg

USA
1378 Posts

Posted - 08/20/2003 :  5:00:02 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Here's a "brute force" approach:

// BEGIN SCRIPT
get %C -e end; // How many "values"?
colnum = wks.c1 + 1; // Which one is the "count" column?
wo -a 1; // Add a new column
row = 1; // Begin with row 1
loop(ii,1,end) { // For each "value"
val = %C[ii]; // Read the current "value"
count = wcol(colnum)[ii]; // Read the "count"
repeat count { // For "count" number of times
wcol(wks.ncols)[row] = val; // Store current "value"
row++; // Increment row
}
}
wo -s; // deselect worksheet
wo -s wks.ncols 0 wks.ncols 0; // Select our new column
run.section(Plot,Histogram); // Run the Plot Histogram script
// END SCRIPT

With your "value" column highlighted (and your "counts" in the next column to the right), run the above script.

For large "counts", the above script may get P - A - I - N - F - U - L - L - Y S - L - O - W, so you might want to go with an OriginC version. (I leave that as an exercise.. )

Go to Top of Page

benderrr

Germany
Posts

Posted - 05/11/2009 :  10:33:49 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi,

I have exactly the same problem of creating a weighted histogram. However, as Greg had already indicated, the script is too slow for my data (and RAM memory on my machine is insufficient). Therefore, and since the problem was discussed more than 5 years ago - is there any faster method, or a routine implemented in newer versions of Origin which can be used to attack the problem?

Thanks in advance,

Bernhard
Go to Top of Page

Iris_Bai

China
Posts

Posted - 05/18/2009 :  11:34:18 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi Bernhard,

Please regerence to the following function:


void PlotWeightedHistogram()
{
	// assume the active worksheet has two columns, first column contains value data, another column contains counts data.
	Worksheet wks = Project.ActiveLayer(); 
	if( wks )
	{
		DataRange dr;
		dr.Add(wks, 0, "Range1");
		dr.Add(wks, 1, "Range2");
		
		vector 		vValues;
		vector		vCounts;
		dr.GetData(&vValues, 0); // 1st range for Values data
		dr.GetData(&vCounts, 1); // 2nd range for Counts data
		
		vector vData;
		for(int ii=0; ii<vCounts.GetSize(); ii++)
		{
			vector vTemp;
			vTemp.SetSize(vCounts[ii]);
			vTemp = (double)vValues[ii];
			vData.Append(vTemp);			
		}
		
		// add a new column and put data from vData to it
		DataRange dr2;
		dr2.Add(wks, wks.AddCol(), "X");
		dr2.SetData(vData);
		
		// plot histogram
		GraphPage gp;
		gp.Create("HIST"); // HIST is the graph template for histogram plot
		
		GraphLayer gl = gp.Layers(-1); // get active layer
		int index = gl.AddPlot(dr2, IDM_PLOT_HISTOGRAM_TYPE);
		if(index >= 0)
		{
			out_str("Successfully plot histogram");
			gl.Rescale();
		}		
	}
}


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