user673218
user673218

Reputation: 411

Check for row or column vector in matlab

I have a function that processes a row vector. I want to make it generic for any type of input vector. Be it column or row. One solution i thought is to preserve the existing implementation and check for the input vector for column or row type. How can i perform this check? iscolumn() or isrow() functions do not work here!

Upvotes: 1

Views: 7736

Answers (2)

user85109
user85109

Reputation:

First, verify that the input IS a vector. isvector can help here. Or, use size, or any of a number of various artifices.

Second, convert your vector to a column vector.

vec = vec(:);

Third, write your code to always expect a column vector, since vec(:) does that.

Finally, save the original shape of your vector and reshape any output vector expected to be the same shape as the input. So your final code should look vaguely like this...

% test for errors
if ~isvector(vec)
  error('The sky is falling')
end

% convert to column form always
vecshape = size(vec);

% process the vector
outputvec = ... % do stuff here

% reshape the output to be the same shape as the input
outputvec = reshape(outputvec,vecshape);

Upvotes: 7

Tamás
Tamás

Reputation: 48071

Check the size of the vector using size - if it has one column and many rows, your function can call itself with the transposed variant.

Upvotes: 2

Related Questions