Borrar filtros
Borrar filtros

Info

La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.

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

1 visualización (últimos 30 días)
function [cd,a,b]=picker(cd,a,b);
if picker(a<b,a,b)
fprintf('out=a');
else
fprintf('out=b')
end
  2 comentarios
evan muas
evan muas el 9 de Dic. de 2019
This is my solution. If you assign cond==1 or true, it still works well.
function out= picker(cond,in1,in2)
if cond==1
out=in1;
else
out=in2;
end
Guillaume
Guillaume el 9 de Dic. de 2019
When cond is logical, if cond == 1 is just a slower way of doing if cond.
cond == 1 converts the logical to a double, compare that double to one to return the exact same logical value you started with.

Respuestas (10)

Stephen23
Stephen23 el 11 de Jul. de 2019
Editada: Stephen23 el 11 de Jul. de 2019
function [cd,a,b]=picker(cd,a,b);
if picker(a<b,a,b)
fprintf('out=a');
else
fprintf('out=b')
end
Your function ignores the first input entirely, and then for no obvious reason it recursively calls itself, providing its first input (which is ignored anyway) as being the output of the comparison a<b (a comparison which is not mentioned anywhere in your assignment). Then you provide three outputs from the function, none of which are the one output that the assignment requested. For no obvious reason you print some data to screen, which is nice, but is irrelevant for the assignment that you were actually given.
You need to read the assignment, understand what it requests, and then implement that... in fact your assignment is so clearly explained, it almost does all of your work for you:
function out = picker(cond,in1,in2);
if ???
out = ???;
else
out = ???;
end
I left you three very simple things for you to complete. (hint: use each input variable once).
  3 comentarios
Guillaume
Guillaume el 11 de Jul. de 2019
Well of course. What else did you expect?
if true
dosomething
else
dosomethingelse
end
is always going to execute dosomething since you're explicitly setting the if condition to true.
Stephen23
Stephen23 el 11 de Jul. de 2019
Editada: Stephen23 el 11 de Jul. de 2019
@Moeez ur Rehman Qureshi: much better, but you should read my answer "use each input variable once": how many inputs does the function have, and how many have you used?

Chech Joseph
Chech Joseph el 3 de Sept. de 2019
function out = picker(condition,int1,int2)
condition = int1 > int2
if condition == 1
out = int1
elseif condition == 0
out = int2
else
return
end
still not pasing both tests. Why?
  2 comentarios
Steven Lord
Steven Lord el 3 de Sept. de 2019
Your function ignores the value its caller passes into it as the first input, throwing the user's input away and replacing it with the result of "int1 > int2". Is that how your assignment states the function should behave?
I don't know if your assignment is different from the one posted by Moeez ur Rehman Qureshi but if it's the same no, your function does not behave the way the assignment states it should.
Rik
Rik el 3 de Sept. de 2019
Also, this is not an answer, but a comment or a question. You should have put it in the comment section.

Ahmed J. Abougarair
Ahmed J. Abougarair el 19 de Abr. de 2020
Editada: Rik el 20 de Abr. de 2020
function out = picker(condition,int1,int2)
y=logical(condition)
if y==1
out = int1;
else
out = int2;
end
end
  3 comentarios
Rik
Rik el 20 de Abr. de 2020
Why are you posting complete solutions to homework questions?
And if y is already a logical, why to you compare it to 1?
Anurag Verma
Anurag Verma el 30 de Mayo de 2020
function out=picker(condition,in1,in2)
if condition==true
in1<in2 && in2>in1
out=in1
else condition==false
in2>in1||in2==in1
out=in2
end

Vishal Natraj
Vishal Natraj el 25 de Abr. de 2020
Editada: Vishal Natraj el 25 de Abr. de 2020
function[out] = picker(condition, in1, in2)
if %fill the conditon on your own
out = % ???;
else
out = %???
end
  5 comentarios
Jun Choi
Jun Choi el 26 de Abr. de 2020
function out = picker(condition,in1,in2)
if condition == 1
out=fprintf('%d\n',in1)
else
out=fprintf('%d\n',in2)
end
@Vishal, Thank you for the explanation. I understand what the correct answer is. Then, I tried to come up with different form of answer above. What is wrong this code?
Stephen23
Stephen23 el 26 de Abr. de 2020
Editada: Stephen23 el 26 de Abr. de 2020
@Jun Choi: the fprintf documentation explains that its output is "...the number of bytes that fprintf writes". Is that what your assignment requests that your function output should be?
Does the assignment require that your function should print something to the command window?

sourab sarkar
sourab sarkar el 23 de Mayo de 2020
Editada: sourab sarkar el 23 de Mayo de 2020
function out=picker(cdn,in1,in2)
if(cdn ==1)
out=in1;
else
out=in2;
end
---------------------

sourab sarkar
sourab sarkar el 23 de Mayo de 2020
As the assignment example explains that the user enters the arguments i.e the condition and value of in1 and in2.So,task is to write a program that can check for all conditions given by the user and produce the aprocreate output.

Roweida Bawab
Roweida Bawab el 23 de Mayo de 2020
This is how I solved it
function out = picker (condition, in1, in2)
if condition == true
out = in1;
else out = in2;
return
end
  2 comentarios
Stephen23
Stephen23 el 23 de Mayo de 2020
Editada: Stephen23 el 23 de Mayo de 2020
Writing condition == true is not required, as explained in this comment:
The return is not required.
It would be much clearer to write out = in2; on its own line.

Anurag Verma
Anurag Verma el 30 de Mayo de 2020
function out=picker(condition,in1,in2)
if condition==true
in1<in2 && in2>in1
out=in1
else condition==false
in2>in1||in2==in1
out=in2
end
this is the code that should be used and do not copy paste plz understand and do.
  1 comentario
Stephen23
Stephen23 el 4 de Jun. de 2020
function out=picker(condition,in1,in2)
if condition==true % superfluous "==true", see Guillaume's comment below the question
in1<in2 && in2>in1 % serves no functional purpose
out=in1 % probably should use trailing semi-colon
else condition==false % superfluous "condition=false", should be simple "else"
in2>in1||in2==in1 % serves no functional purpose
out=in2 % probably should use trailing semi-colon
end
Note that the syntax
else X
is actually equivalent to
else
X
where X is simply displayed and serves no functional purpose.

AYUSH MISHRA
AYUSH MISHRA el 4 de Jun. de 2020
function out=picker(condition,in1,in2)
if condition==true
out=in1;
else out=in2;
end
SOLUTION :
picker(true,1,2)
ans =
1
picker(false,1,2)
ans =
2

ROHAN SUTRADHAR
ROHAN SUTRADHAR el 6 de Jun. de 2020
function out = picker(condition,in1,in2)
if condition==0
out = in2;
else
out = in1;
end

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by