Writing function for fliping vectors
Show older comments
So I got these instructions:
Write a function flipvec that will receive one input argument. If the input argument is a row vector, the function will reverse the order and return a new row vector. If the input argument is a column vector, the function will reverse the order and return a new column vector. If the input argument is a matrix or scalar, the function will return the input argument unchanged.
So i wrote this code:
function out = flipvec(~)
out = input('Enter a column or row vector, a matrix or a scalar: \n');
m = ismatrix(out);
s = isscalar(out);
CR = isvector(out(1,:));
if s == 1
disp(out)
elseif s ==0
if m == 1
disp(out)
elseif m == 0
if CR == 1
newout = fliplr(out);
disp(newout)
elseif CR == 0
vecout = flipud(out);
disp(vecout)
else
disp('Quit.')
end
end
else
disp('Quit.')
clear
end
These are some of the outputs I get when I call the function:
>> flipvec
Enter a column or row vector, a matrix or a scalar:
3
3
ans =
3
>> flipvec
Enter a column or row vector, a matrix or a scalar:
3
3
ans =
3
>> flipvec
Enter a column or row vector, a matrix or a scalar:
3
3
ans =
3
>> flipvec
Enter a column or row vector, a matrix or a scalar:
[3; 5; 7]
3
5
7
ans =
3
5
7
>> flipvec
Enter a column or row vector, a matrix or a scalar:
[3 5; 6 5; 9 10]
3 5
6 5
9 10
ans =
3 5
6 5
9 10
>> flipvec
Enter a column or row vector, a matrix or a scalar:
[2 4 5 6]
2 4 5 6
ans =
2 4 5 6
I'm not sure why the function is not fliping the vectors or also displaying an "ans." Any suggestions?
Thank you!!
3 Comments
Victoria Gonzalez Canalle
on 9 Feb 2020
Steven Lord
on 10 Feb 2020
Do not call input in your code. You don't need it; the grading software (which could be your professor or teaching assistant) will call your function with an input argument. Just use that input argument instead of prompting them to enter the input again.
Your ismatrix logic is flawed for the reason already explained here:
Accepted Answer
More Answers (1)
Steven Lord
on 7 Feb 2020
Your function fails to satisfy the requirements of your question in several ways. Let's look at the first two sentences.
Write a function flipvec that will receive one input argument.
Your function does technically satisfy this requirement, though ...
If the input argument is a row vector, the function will reverse the order and return a new row vector.
your function fails this requirement in two different ways. The first is that it completely ignores the input argument with which it was called. It instead asks the user to input another array and operates on that array. To satisfy this requirement, don't ignore the input argument -- just remove the input statement.
The second is that it does not return the array that has been reversed (or not reversed, if it's a matrix.) Instead it returns the array that you asked the user to input. The reversed array is called newout or vecout but the contents of the vector out will be returned from your function. That's why the function doesn't appear to flip the vector -- it does, but throws away the flipped vector and returns the vector entered at the input prompt. To satisfy this requirement, return the correct variable from your function (or assign the thing you want returned to the correct variable, to phrase that a bit differently.)
Now looking at some sections of your code:
CR = isvector(out(1,:));
What happens if you call your function with the array [] (or pass it in as an input argument?) You've made an assumption, that out has a first row, and that's not always a valid assumption. You could check the output of the size function, but unless your assignment prohibits using them I'd suggest isrow and iscolumn instead.
disp(out)
Nowhere in the text of the assignment does it state that you should display anything. It states that your function should return something, but you don't have to display something in order to return it. You just have to specify it in your function definition.
if s == 1
disp(out)
elseif s ==0
The only thing the isscalar function included in MATLAB (which is what you used to create s) can return are the logical values false (0) or true (1) so the check that s is 0 if it's not 1 isn't really necessary. You could change the elseif into an else.
clear
This is unnecessary. When a function exits [*], its workspace is destroyed after anything that the function returns has been returned to its caller. [* For simplicity I'm ignoring nested functions in this discussion.] MATLAB will clear up after itself.
As for why you receive ans as output, that's the documented and expected behavior. Call your function with an output argument and/or with a semicolon at the end of the function call if you want to avoid assignments to ans. See the "Result of a Simple Calculation" example on that documentation page; the line that defines result doesn't create or update ans.
Categories
Find more on Matrices and Arrays 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!