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
 `if` function conditional argument
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

lijbereket

14 Posts

Posted - 07/28/2022 :  11:57:13 AM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
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


Edited by - lijbereket on 07/28/2022 11:59:09 AM

snowli

USA
1412 Posts

Posted - 07/28/2022 :  12:12:52 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
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
Go to Top of Page

lijbereket

14 Posts

Posted - 07/28/2022 :  12:47:12 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply

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.
Go to Top of Page

snowli

USA
1412 Posts

Posted - 07/28/2022 :  4:37:35 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
We will improve the if function doc.

Thanks, Snow
Go to Top of Page

minimax

355 Posts

Posted - 07/28/2022 :  10:30:42 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
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
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