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

8 Kommentare

Mukti Awad
Mukti Awad am 16 Aug. 2019
This code gives error
Assessment result: incorrectFalse
Variable out has an incorrect value.
function out = picker(condition,in1,in2)
if condition == 1;
out = in1;
else
out = in2;
end
\\try this code
Purushottam Shrestha
Purushottam Shrestha am 23 Jul. 2020
right.
function out=picker(condition,in1,in2)
if condition==true
out=in1;
else
out=in2;
end
it is a simplest code that can execute your program.
Walter Roberson
Walter Roberson am 11 Sep. 2020
No, that is not the simplest ;-)
Aniket Kumar
Aniket Kumar am 2 Okt. 2020
Jaideep has written the right code
Harish C S
Harish C S am 29 Mär. 2021
this is wrong because everytime matlab thinks it is true
Rik
Rik am 29 Mär. 2021
@Harish C S That is why it is a question. If it were correct, why bother posting the question? See the comments and answers in this thread for working code.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Star Strider
Star Strider am 14 Jun. 2019

2 Stimmen

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!

4 Kommentare

Correct code is:
function out = picker(condition,in1,in2)
if condition
out = in1;
else
out = in2;
end
end
Miguel Viñeza
Miguel Viñeza am 23 Mär. 2020
thankyouu!!
Priya Narayan
Priya Narayan am 17 Jun. 2020
This code is not running.
Rik
Rik am 17 Jun. 2020
Which code? And as Star Strider suggests: check your spelling, including in the code he posted.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (4)

Sneham Shrikant Vaidya
Sneham Shrikant Vaidya am 18 Jul. 2020

2 Stimmen

function out = picker(condition,in1,in2)
if condition == 1;
out = in1;
else
out = in2;
end
\\try this above code
Ahmed Salmi
Ahmed Salmi am 17 Jul. 2020

1 Stimme

function out=picker(condition,in1,in2)
if condition==true
out = in1;
elseif condition==false
out = in2;
end
end

1 Kommentar

Walter Roberson
Walter Roberson am 31 Aug. 2020
Under what circumstances can it be the case that the condition is not true and also is not false? When you use elseif you imply that there are cases where nothing that has been tested before came out true and also that the current test might not come out true either.

Melden Sie sich an, um zu kommentieren.

suat karabocek
suat karabocek am 20 Jun. 2019

0 Stimmen

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

4 Kommentare

KETAN PATEL
KETAN PATEL am 21 Jun. 2019
Thanks!!!
function out = picker(a,b)
if(a<b)
fprintf("%d",a);
else
fprintf("%d",b);
end
what the problem on this code
Ahmed J. Abougarair
Ahmed J. Abougarair am 19 Apr. 2020
The picker function required three input argument
DGM
DGM am 4 Mär. 2023
Bearbeitet: DGM am 4 Mär. 2023
You may use two if conditions with numeric comparison, but why should you?
if condition == 1
out = in1;
elseif condition == 0
out = in2;
% else
% all other cases are unhandled and will cause the function
% to exit without assigning a value to the output
end
The assignment asserts that condition is a variable of class 'logical'. While the comparisons using numeric values will implicitly work with logical inputs, you're designing your code around handling logical variables as numeric variables. Your code now accepts any numeric input and will fail if it is neither 0 nor 1.
If your inputs are supposed to be logical, make sure they're logical or converted to logical.
Also, an else statement doesn't accept a condition; an elseif does.

Melden Sie sich an, um zu kommentieren.

amjad khan
amjad khan am 9 Apr. 2020
Bearbeitet: Walter Roberson am 31 Aug. 2020

0 Stimmen

function out(condition,in1,in2)
if condition>0 % it is logical
out=in1;
else
out=2;
end

1 Kommentar

DGM
DGM am 4 Mär. 2023
The only significant difference between this code and the other examples is that this one doesn't actually work. It may be a minor and obvious error, but the fact that it's obvious indicates that this wasn't tested.
Why post untested code that's not meaningfully different than other examples?
You do have one comment in the code, so I'll give you credit for that.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu App Building finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 14 Jun. 2019

Bearbeitet:

DGM
am 4 Mär. 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by