Writing a function that accepts input argument that is one of 3 things
16 views (last 30 days)
Show older comments
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
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.
Accepted Answer
Sam Chak
on 20 Mar 2024
Hi @Kyle Weaver
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)
%% Perform Test 2
input2 = 1234;
testInput(input2)
%% Perform Test 3
input3 = string(input1);
testInput(input3)
%% 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
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
[numRows, numCols] = size(A)
%% A row vector
A = [11, 22]
[numRows, numCols] = size(A)
%% A column vector
A = [11; 33]
[numRows, numCols] = size(A)
%% A matrix
A = [11, 22; 33, 44]
[numRows, numCols] = size(A)
More Answers (1)
Matt J
on 19 Mar 2024
Edited: Matt J
on 19 Mar 2024
7 Comments
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
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!