Writing a function that accepts input argument that is one of 3 things
Mostra commenti meno recenti
Given: Write a function that accepts an input argument that is one of 3 things: a vector, a matrix, or a scalar of values. The function should return an output argument that contains a character array that reads: 'vector', 'scalar', or 'matrix'
Find: y
Issue: I am unfamiliar with what the question is asking. Does it want me to print the results of y using the fprintf function? Or...
My solution: Code used to call function is below, I am trying to extrapolate what y in the function should be. But it doesn't make sense =[
function
y=s
y1=char(vector)
y=rv
y2=char(scalar)
y=cv
y3=char(matrix)
end
%Code used to call function:
%s=5;
%y=findargtype(s)
%rv=randi(10,1,5);
%y=findargtype(rv)
%cv=randi(10,5,1);
%y=findargtype(cv)
%M=randi(10,randi([4,8]));
%y=findargtype(M)
8 Commenti
Stephen23
il 20 Mar 2024
"I am unfamiliar with what the question is asking. Does it want me to print the results of y using the fprintf function?"
Does the assignment state that somewhere? Your assignment does state that the function should return an output, which should be a character vector. So ... returning an output which is a character vector would probably be the best.
Stephen23
il 20 Mar 2024
"I am just unsure of how to get a single input argument to be 3 things all at once"
The output is not three things all at once (you invented that yourself). The task is very simple:
- if the input is a scalar, then return the text SCALAR
- if the input is a vector, then return the text VECTOR
- if the input is a matrix then return the text MATRIX
You are overthinking this.
Spaceman
il 20 Mar 2024
Stephen23
il 20 Mar 2024
"I can't fathom what to actually put INSIDE of the function to get it to call properly."
Perhaps the most basic code for deciding between different things is IF .. ELSEIF... ELSE.
MattJ already told you the names of some functions that could be useful.
Spaceman
il 20 Mar 2024
Stephen23
il 20 Mar 2024
"I know this isn't right, but I am heading in the right direction I hope."
Sure, the direction looks good.
- the assignment asked you to return an output argument, not display something.
- use the functions Matt J listed for you
- forget about s, rv, cv, and M: they only exist outside the function. You have one input named x.
Spaceman
il 21 Mar 2024
Risposta accettata
Più risposte (1)
7 Commenti
Spaceman
il 20 Mar 2024
Stephen23
il 20 Mar 2024
"Does it want me to set it up as you have, where the output changes every time,..."
Yes.
"...or set up 3 different unique input/output variables that returns a charater array that reads 'vector', 'scalar', 'matrix'?"
It is unclear what you mean, but it is very unlikely that is what is being requested.
Why are you putting all of the code that is used for testing this function... inside the function itself?
Start again. The function should not contain the test code. The function should contain your code that checks the size of the input argument and (based on that size) defines a character vector (which you will return as the single output argument).
Spaceman
il 20 Mar 2024
Walter Roberson
il 20 Mar 2024
s=5;
y=findargtype(s)
rv=randi(10,1,5);
y=findargtype(rv)
cv=randi(10,5,1);
y=findargtype(cv)
M=randi(10,randi([4,8]));
y=findargtype(M)
function result = findargtype(x)
some code goes here that tests x and determines whether it is
a scalar, a vector, or a matrix and assigns an appropriate
character vector to 'result'
end
Spaceman
il 8 Apr 2024
Categorie
Scopri di più su Descriptive Statistics in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!