MATLAB Answers

0

if-statement with conditions.

Asked by KETAN PATEL on 14 Jun 2019
Latest activity Commented on by Selvakumar T on 6 Sep 2019
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

  1 Comment

Mukti Awad on 16 Aug 2019
This code gives error
Assessment result: incorrectFalse
Variable out has an incorrect value.

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!

  1 Comment

Mukti Awad on 18 Aug 2019
Correct code is:
function out = picker(condition,in1,in2)
if condition
out = in1;
else
out = in2;
end
end

Sign in to comment.


Answer by suat karabocek on 20 Jun 2019

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

  2 Comments

Thanks!!!
function out = picker(a,b)
if(a<b)
fprintf("%d",a);
else
fprintf("%d",b);
end
what the problem on this code

Sign in to comment.