Clear Filters
Clear Filters

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

4 views (last 30 days)
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 Comments
Stephen23
Stephen23 on 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
Spaceman on 21 Mar 2024
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.

Sign in to comment.

Accepted Answer

Sam Chak
Sam Chak on 20 Mar 2024
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 Comments
Sam Chak
Sam Chak on 20 Mar 2024
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
Spaceman
Spaceman on 21 Mar 2024
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.

Sign in to comment.

More Answers (1)

Matt J
Matt J on 19 Mar 2024
Edited: Matt J on 19 Mar 2024
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 Comments
Walter Roberson
Walter Roberson on 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
Spaceman on 8 Apr 2024
Genius. This is eloquently written. I just wish it was as intuitive to me as it might be for a more seasoned coder.

Sign in to comment.

Tags

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!