convert vector of n or any size into a matrix
31 views (last 30 days)
Show older comments
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?
0 Comments
Accepted Answer
Jon
on 20 Mar 2018
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
Guillaume
on 20 Mar 2018
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.
Jon
on 20 Mar 2018
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.
More Answers (2)
Walter Roberson
on 20 Mar 2018
If you have the signal processing toolbox then you can use buffer()
0 Comments
Christian
on 25 Mar 2018
2 Comments
Walter Roberson
on 25 Mar 2018
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.
See Also
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!