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

16 visualizaciones (últimos 30 días)
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 comentarios
Stephen23
Stephen23 el 20 de Mzo. de 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 el 21 de Mzo. de 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.

Iniciar sesión para comentar.

Respuesta aceptada

Sam Chak
Sam Chak el 20 de Mzo. de 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 comentarios
Sam Chak
Sam Chak el 20 de Mzo. de 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 el 21 de Mzo. de 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.

Iniciar sesión para comentar.

Más respuestas (1)

Matt J
Matt J el 19 de Mzo. de 2024
Editada: Matt J el 19 de Mzo. de 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 comentarios
Walter Roberson
Walter Roberson el 20 de Mzo. de 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 el 8 de Abr. de 2024
Genius. This is eloquently written. I just wish it was as intuitive to me as it might be for a more seasoned coder.

Iniciar sesión para comentar.

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by