convert vector of n or any size into a matrix

Please help me to understand, I have a vector with n elements (which I can know with length), I would like to build a matrix with n / x rows and x columns (where x is a number that I have chosen). If n / x is integer ok but while if not, can I find the nearest whole integer and fill the missing electives with zeros? How could I do?

 Accepted Answer

Here's one way to do this. Note that I changed your notation to use n (rather than x) for the number of columns as this seems more conventional. I think there is some arbitrariness in where you put the zeros for the missing elements. Here I put them at the end of the vector, and then wrap the vector columnwise. By the way, what is the application for this if you don't mind sharing?
function A = reshapevec(v,n)
% reshape an arbitrary length vector into a matrix, with n columns, putting in zeros for
% missing elements if length of v does not divide by x
% find number of elements, in input vector
numElements = length(v);
% divide and round up (ceil) to get required number of rows, m
numRows = ceil(numElements/n);
% determine total number of elements,needed in the desired m x n array
numNeeded = numRows*n;
% determine number of missing elements (how many are we short by)
numMissing = numNeeded - numElements;
% concatenate additional needed zero elements
vc = [v;zeros(numMissing,1)];
% now reshape (wrap columnwise) to get a matrix of the desired size
A = reshape(vc,numRows,n);

2 Comments

vc = [v(:); zeros(numMissing, 1)];
would be a lot safer. As it is the code only works with column vectors. In the same vein, I'd recommend numel rather than length or use a validateattributes to make sure the input is vector.
I agree, making it independent of whether the input vector is entered as a row or column is always nice and I usually do that too in my own code. I was just trying to outline the approach, and not necessarily give the original poster production quality code. Specifically I didn't want to add lots of additional aspects for "bullet proofing" that maybe diverted attention from the main question. That said, I guess we shouldn't waste an opportunity to show best practices if it's as simple as using a colon to insure a column.

Sign in to comment.

More Answers (2)

If you have the signal processing toolbox then you can use buffer()
Yes, sure, thanks for the answers, now I want to think about this. The problem is to use a matrix and not a vector so as to recall (during nested cycles) some rows or columns easily using indexes (i, j). My difficulty is to have as solutions of the vectors X = x(1).....x(n), x(n + 1).......x(2n).....x(end ) while I prefer X = [x (1) x (2) x (n)] 'as a form.

2 Comments

buffer() will return a matrix in which the columns are from the consecutive elements. buffer() automatically pads the last entry if needed, and buffer() handles overlaps as well for the cases where you need a sliding window.
Ok thanks, now I implement this method, I try to understand if it fits better my problem.

Sign in to comment.

Categories

Products

Asked:

on 19 Mar 2018

Commented:

on 26 Mar 2018

Community Treasure Hunt

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

Start Hunting!