MATLAB Answers

0

if-statement with conditions.

Asked by KETAN PATEL on 14 Jun 2019
Latest activity Commented on by KETAN PATEL on 21 Jun 2019 at 4:56
Write a function called picker that takes three input arguments called condition, in1 and in2 in this order. The argument condition is a logical. If it is true, the function assigns the value of in1 to the output argument out, otherwise, it assigns the value of in2 to out.
This is a problem from a course that I am taking on Coursera. I get a correct output for "true" but the output for the "false" condition is always wrong. Please, any kind of help will highly appreciated.
Annotation 2019-06-14 203145.png
Here is my code:
function out = picker(condition,in1,in2)
if true
out = in1;
else
out = in2;
end
end

  0 Comments

Sign in to comment.

2 Answers

Answer by Star Strider
on 14 Jun 2019
 Accepted Answer

You can safely delete this assignment:
condintion = in1 < in2;
because it overwrites the ‘condition’ argument.
You can also just use:
if conditon
You don’t have to test it again.
Also, check your spelling!

  0 Comments

Sign in to comment.


Answer by suat karabocek on 20 Jun 2019 at 18:49

you may use if and else and two conditions including 1 and 0. such as;
your function.........
if condition == 1;
....
....
else condition == 0;
....
....
end

  1 Comment

Thanks!!!

Sign in to comment.