Writing a function that accepts input argument that is one of 3 things

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

"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.
I am just unsure of how to get a single input argument to be 3 things all at once: Scalar, vector, matrix. Once again, functions have defeated me. I can easily feed a function 3 output arguments that accept however many input arguments. Normally these output arguments are equations, scalars, vectors, matrices, etc. But I am uneducated how to return an output argument that contains a character array that reads: 'vector', 'scalar', or 'matrix...
"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.
I feel so goofy. I just don't understand how do to that here. I tried to do what Matt J. illustrated but was unable to gather the desired results. Given the code to call the function, I can't fathom what to actually put INSIDE of the function to get it to call properly.
"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.
Wow. I can't believe I didn't think about using control structures...
function y = findargtype(x)
if y=s
disp('Scalar')
elseif y=rv
disp('Vector')
elseif y=cv
disp('Vector')
elseif y=M
disp('Matrix')
end
I know this isn't right, but I am heading in the right direction I hope.
"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.
I am utterly in disbelief about how simple the solution was. Once I see it, it all clicks. Of course. I was focused too much on the function call and not what I should of been worried about which is DEFINING in the function that which constitutes a scalar, matrix, vector, etc... Then of course using '' to denote a character array with the type I defined contained within the variable y. Coding is seriously an art, and a heARTache all in one.

Accedi per commentare.

 Risposta accettata

The assignment typically requires designing the Flow of Execution before starting to write the code, but it doesn't explicitly specify which step to begin with. It's similar to when students are given a math problem and asked to solve it. Some individuals excel at activating their neurons to explore solutions in uncharted territories, while others excel at forming synaptic connections to recall successful past experiences.
It seems that you may be good at learning through examples. If that's the case, you can refer to this simple example to test whether the input is a text or a number. Although it doesn't directly solve your problem, it should provide you with some insights into the Flow of Execution within the function.
%% Perform Test 1
input1 = 'Kyle';
testInput(input1)
ans = 'The input is a character array.'
%% Perform Test 2
input2 = 1234;
testInput(input2)
ans = 'The input is a numeric array.'
%% Perform Test 3
input3 = string(input1);
testInput(input3)
Warning: The input is an invalid input.
%% The function code determine the type of input
function result = testInput(inArg)
% Stage 1: Carry out two tests
ToF1 = ischar(inArg); % True or False test if input is a character array
ToF2 = isnumeric(inArg); % True or False test if input is a numeric array
% Stage 2: Show result if a specific Condition is met
if ToF1 == 1 & ToF2 == 0; % Condition 1
result = 'The input is a character array.';
elseif ToF1 == 0 & ToF2 == 1; % Condition 2
result = 'The input is a numeric array.';
else % Condition 3
warning('The input is an invalid input.');
end
end

2 Commenti

By the way, I'm unsure whether the assignment permits the use of specific functions. The main objective is probably to determine the characteristics of the input and intuitively identify the patterns (generally cannot be taught) within those characteristics in order to proceed with setting up the If–Then rules to play the game.
The concept of Scalars, Vectors, Matrices, and Tensors is commonly covered in the introduction to Linear Algebra. Here are the defining characteristics of a Scalar, a Vector, and a Matrix:
%% A scalar
A = 11
A = 11
[numRows, numCols] = size(A)
numRows = 1
numCols = 1
%% A row vector
A = [11, 22]
A = 1×2
11 22
[numRows, numCols] = size(A)
numRows = 1
numCols = 2
%% A column vector
A = [11; 33]
A = 2×1
11 33
[numRows, numCols] = size(A)
numRows = 2
numCols = 1
%% A matrix
A = [11, 22; 33, 44]
A = 2×2
11 22 33 44
[numRows, numCols] = size(A)
numRows = 2
numCols = 2
This helped immensely. I was dramatically overthinking the problem, as I often do. When I see the solution it certainly unlocks that door that was being slammed shut every time I would think of a solution that made sense to me. I will put the answer I found on here.

Accedi per commentare.

Più risposte (1)

Does it want me to print the results of y using the fprintf function?
No, it wants you to write the function fcn so that it behaves as below,
out=fcn(rand(3))
out = 'matrix'
out=fcn(4)
out = 'scalar'
out=fcn(1:7)
out = 'isvector'
You should read up on the functions ismatrix, isvector, isscalar.
function output = fcn(input)
....
end

7 Commenti

This makes sense. I should be able to figure it out from here after reviewing the documentation... I am just confused by the question wording. Does it want me to set it up as you have, where the output changes every time, or set up 3 different unique input/output variables that returns a charater array that reads 'vector', 'scalar', 'matrix'?
"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.
I think I get it. s would be the scalar value, rv and cv are both vectors, and M is a matrix. So function should be: ??? I think I am still confused.
function y=inputargtype(x)
y=Fcn(s)
y=Fcn(rv)
y=Fcn(cv)
y=Fcn(M)
end
% Code 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)
Something just seems odd about it. I know how to get a function to return output arguments with provided input arguments... But this is another beast. I am not using x anywhere as an input as you can see in the code to call my function. I am also not well versed in the 'Fcn' function if that's what we are using here to produce the character array.
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).
My apologies, I thought that was how the function would run it. Firstly, y would be the fcn(s) which would return scalar, secondly y would be fcn(rv) which would return vector, etc... (in my mind it made sense). I might have to just sleep on it and come back refreshed; but it'll be another night of failed code nightmares.
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
Genius. This is eloquently written. I just wish it was as intuitive to me as it might be for a more seasoned coder.

Accedi per commentare.

Prodotti

Release

R2023b

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by