MATLAB: Writing function for fliping vectors

flipfunctionvector

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

Best Answer

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.