The Origin Forum
File Exchange
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
 LabTalk Forum
 Autofill Column with Last Known Value
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

Drbobshepherd

USA
Posts

Posted - 04/02/2012 :  6:03:00 PM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
Is there a Labtalk routine or an X-function to fill cells with the last known value? For example,

col(B)={0 -- -- -- -- -- -- -- 1 -- -- -- -- 12 -- -- 0 --}.

I would like to process so

col(B)={0 0 0 0 0 0 0 0 1 1 1 1 1 12 12 12 0 0}.

I can have hundreds of thousands of missing values to fill so I'd prefer not to resort to nested loops, which can be quite slow.

DrBobShepherd


Origin Ver. and Service Release (Select Help-->About Origin):OriginPro 8.6.0
Operating System:Window XP

Hideo Fujii

USA
992 Posts

Posted - 04/04/2012 :  4:18:34 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi Drbobshepherd,

How about the following formula in the Set Column Values tool?

col(B)[i]==1/0?col(B)[i-1]:col(B)[i]

--Hideo Fujii
OriginLab
Go to Top of Page

Drbobshepherd

USA
Posts

Posted - 05/02/2012 :  10:34:53 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hideo,

Thanks, your command works perfectly. I see that it is basically an if statement:

if(col(B)[i]==NaN) col(B)[i]=col(B)[i-1];

but I am not familiar with the language. Do you have a reference I could use to learn it?

DrBobShepherd
Go to Top of Page

couturier

France
106 Posts

Posted - 05/02/2012 :  11:04:04 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
It is more an if - else statement, used in variable assignement. General syntax is:

variable=condition?value1:value2;
--> variable will be assigned to value1 if condition is true, or value2 if condition if false.

if your case, full corresponding command is:
col(B)[i]=col(B)[i]==1/0?col(B)[i-1]:col(B)[i];
which mean:
if col(b)[i] is NaN, put col(b)[i-1] into col(b)[i]
else put col(b)[i] into col(b)[i]

Search for "conditionnal operator" into labtalk help
Go to Top of Page

couturier

France
106 Posts

Posted - 05/02/2012 :  11:11:05 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
note that it can also be used for vector assignement.

For example:

range r1=1, r2=2;
r1=uniform(100000);
r2=r1>0.5?1:0;
Go to Top of Page

Drbobshepherd

USA
Posts

Posted - 05/02/2012 :  2:29:57 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Couturier,
I found the reference in Help/Programming/Main/Operators/Conditional. Hard to believe I have been programming for decades without ever being aware of this useful operator. It just fell through the cracks. This proves I don't know everything, but with your help, I am closer.

Dr. Bob
Go to Top of Page

Hideo Fujii

USA
992 Posts

Posted - 05/03/2012 :  11:03:33 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi Couturier,

I also want to say thank you. That vector expression is a surprising rediscovery for me, too.

--Hideo

P.S.
For my curiosity, I have compared the speed of Couturier's vector and If statement.
////////////////////////////
sec;
range r1=1, r2=2;
r1=uniform(100000);
r2=r1>0.5?1:0;
sec -e time;
type -a Vector: $(time) sec;
//
window -t wks;
sec;
range r1=1, r2=2;
r1=uniform(100000);
for(ii=1; ii<=100000; ii++) {
  if(r1[ii]>0.5) r2[ii]=1; else r2[ii]=0;
}
sec -e time;
type -a IF: $(time) sec;

Vector: 0.188 sec
IF: 13.338 sec
So, the conditional operator for vectors is 70(=13.34/0.19) times faster!

Edited by - Hideo Fujii on 05/03/2012 11:38:44 AM
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 © 2008 Originlab Corporation Go To Top Of Page
Snitz Forums 2000