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
 Origin Forum
 `if` function conditional argument

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
lijbereket Posted - 07/28/2022 : 11:57:13 AM
Origin Ver. and Service Release (Select Help-->About Origin): 2022
Operating System: Windows 10

A rather simple question: I used the following expression in the formula row of my worksheet, but don't understand why it works:
if(diff(A,2),0,B)
(see screenshot below)

The first argument of the `if` statement is supposed to be a conditional expression comparing one value to another. `diff` simply takes in a vector and returns a range of differences between adjacent elements. It does not compare values, so I don't get why Origin is not giving me an error.

Thank you

4   L A T E S T    R E P L I E S    (Newest First)
minimax Posted - 07/28/2022 : 10:30:42 PM
It is not a specific treatment in Origin.

Most programming language or products (like C, C++, Python, Excel) will work like that for if statement.


#python script
a=2
if(a):
 print(f'hello {a}')
else:
 print(f'world {a}')
b=0
if(b):
 print(f'hello {b}')
else:
 print(f'world {b}')
c='test'
if(c):
 print(f'hello {c}')
else:
 print(f'world {c}')
d=''
if(d):
 print(f'hello {d}')
else:
 print(f'world {d}')

output is

hello 2
world 0
hello test
world
snowli Posted - 07/28/2022 : 4:37:35 PM
We will improve the if function doc.

Thanks, Snow
lijbereket Posted - 07/28/2022 : 12:47:12 PM

I guess there is some subtilty to the `if` function. A non-zero number in the conditional is treated as being True; and zero is treated as false. I would have thought it should have been written
if(diff(A,2)>0,0,B)


Thank you for the reply, @snowli.
snowli Posted - 07/28/2022 : 12:12:52 PM
U can add a new column and enter diff(A,2) in F(x) cell to see the the output of diff(A,2) in each row.

Regarding to if(diff(A,2),0,B)
as u said, 1st argument is condition string. As long as it's true, it will output 2nd arguemnt. If false, it will output 3rd argument.

So in your case, for each row,
if diff(A,2) is >0 (TRUE), it will output 0.
if it's 0 (FALSE), it will output B value.

See doc of diff() and if()
https://www.originlab.com/doc/LabTalk/ref/Diff-func
https://www.originlab.com/doc/en/LabTalk/ref/If-func


Thanks, Snow

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