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
 Dataset, vector problem
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

schosch100

2 Posts

Posted - 07/02/2008 :  05:50:27 AM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
Hi

I have a worksheet that contains 3 columns. In the first column there are my values, which switch randomly betweent 1 and 0.
Now I need a script that counts the same value (1 or 0) for one period and puts the sum in the next columns. Like:

My values /NEED count for value:1 /count for value:0
1 -------/-- 3 -------------------/--- 2
1 -------/-- 2 -------------------/--- 1
1
0
0
1
1
0
...

I tried to write a origin c program with a while loop but I was not successful :( please help

thx, schosch

cjfraz

USA
22 Posts

Posted - 07/05/2008 :  11:48:52 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply

There might be a more parsimonious solution, but I think this code works as per your description of the problem.

Make sure that the worksheet with the input data is active and all three columns exist before you run it.

J...


void cb(){	

	//make some datasets attached to columns of active sheet as you described.
	Worksheet wks=Project.ActiveLayer();
	Dataset dsIn, ds1, ds0;	
	
	dsIn.Attach(wks,0);
	ds1.Attach(wks, 1);
	ds0.Attach(wks, 2);

	ds0.SetSize(dsIn.GetSize());
	ds1.SetSize(dsIn.GetSize());
	ds0=NANUM;
	ds1=NANUM;
	
	//declare some counters.
	int iOneCount=0;  //number of 'periods of 1s' observed.
	int iZeroCount=0; //number of 'periods of 0s' observed.
	int iSame=1;      //number of observations in current period.
	
	for (int j=1;j<dsIn.GetSize();j++){  //progress through entire input column.
		if (dsIn[j]!=dsIn[j-1]){  //if current value does not equal previous, change has occured.
		
			if (dsIn[j-1]==1){ //change was from 1 to 0 so output to 1s counter.
				ds1[iOneCount++]=iSame;
			}
			else{			   //change was from 0 to 1 so output to 0s coutner.
				ds0[iZeroCount++]=iSame;
			}			
			iSame=1;  //output of period complete, so reset streak counter.
		}
		else	iSame++;	//no change occured, so increment streak counter.	
	}
	
	//finally, take care of the last period.
	if (dsIn[j-1]==1){
		ds1[iOneCount++]=iSame; 
	}
	else{
		ds0[iZeroCount++]=iSame;
	}
	ds0.SetSize(iZeroCount);
	ds1.SetSize(iOneCount);	
	
}

Edited by - cjfraz on 07/05/2008 11:58:42 PM
Go to Top of Page

schosch100

2 Posts

Posted - 07/07/2008 :  02:24:42 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
it works,

thank you!!!
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