Author |
Topic  |
|
lijbereket
14 Posts |
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
 |
Edited by - lijbereket on 07/28/2022 11:59:09 AM |
|
snowli
USA
1412 Posts |
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 |
 |
|
lijbereket
14 Posts |
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
USA
1412 Posts |
Posted - 07/28/2022 : 4:37:35 PM
|
We will improve the if function doc.
Thanks, Snow |
 |
|
minimax
355 Posts |
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 |
 |
|
|
Topic  |
|
|
|