Writing function for fliping vectors

5 views (last 30 days)
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
Steven Lord
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.

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 7 Feb 2020
Edited: Stephen23 on 7 Feb 2020
The main problem is that you are not returning the variable that you flip. You ignored the input argument (which already breaks the requirements of your assignment) and request user input on the second line of the function:
function out = flipvec(~)
out = input('Enter a column or row vector, a matrix or a scalar: \n');
... nothing in the rest of your function changes OUT, so it gets returned unchanged!
Note how out remains completely unchanged in the rest of your function. If you want out to be changed before returning it, then you need to change it, otherwise it will be returned exactly as the user provided it on line two. Also note that writing
if somecondition
elseif not_somecondition
end
is pointless, all you need is
if somecondition
else
end
Note that your assignment does not tell you to display anything, so get rid of all of those disp calls.
You should be doing something like this (to make it simpler I defined the output argument to be the same as the input argument, thus making the scalar and matrix cases simpler):
function out = flipvec(out)
if isscalar(out)
% do nothing
elseif ismatrix(out)
% do nothing
elseif isvector(out(1,:)) % when do you expect this NOT to be a vector?
out = fliplr(out);
else
out = flipud(out);
end
Note that your test for the vector orientation will not work as you expect... when is out(1,:) not a vector?
clear at the end of the function is pointless and will destroy any possibility of returning a useful output. Get rid of it.
  2 Comments
Victoria Gonzalez Canalle
while I understand why you are equalizing th input and output arguments, I was given this code to begin with:
function out = flipvec(in)
out = ???
end
This means that I have to go on from here and cannot really change the input or output arguments. How do you suggest going on from here? I am a beginner and function writing is still a little confusing to me.
Stephen23
Stephen23 on 9 Feb 2020
Edited: Stephen23 on 10 Feb 2020
function out = flipvec(in)
if isscalar(in)
out = in;
elseif is???(in)
out = flip???(in);
elseif is???(in)
out = flip???(in);
else
out = in;
end
end
Now you only have fix the logical conditions, which Steve Lord already gave advice on.
Note that a matrix with one row or one column is also a matrix, so will return true from ismatrix... yet you have been asked to handle those cases differently (as vectors, which are just special cases of matrix). In fact, a 1x1 array is also a matrix, altough we also give it other special names (like "scalar" or "number"), but really a scalar is just a perfectly good 1x1 matrix... and so it also returns true from ismatrix.
To put it another way, there is nothing in the definition of "matrix" that says it must have >=2 columns and >=2 rows!
So if you think through the logic, using ismatrix is not so useful for you here.

Sign in to comment.

More Answers (1)

Steven Lord
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!