I have a vector of length 3. i want to represent it as a matrix of dimension 4*2. ie) if the length of vector is n then matrix should be of dimension (n+1)*2. The matrix should have elements arranged as follows:
Vector= [2 3 4] Matrix = [0 2;2 3;3 4;4 0]
reshape from Matlab is the basis of my answer to your question:
B = reshape(A,m,n) returns the m-by-n matrix B whose elements are taken column-wise from A. An error results if A does not have m*n elements (from the official Matlab help).
You basically add zeros at the beginning and at the end and then have every number in the vector occur twice (if you "unfold"/reshape the matrix). So lets construct the desired matrix by reversing this description:
%set input vector v = [2 3 4]; %"double" the numbers, v_ is my temporary storage variable v_ = [v; v]; %align all numbers along one dimension v_ = reshape(v_, 2*length(v), 1) %add zeros at beginning and end v_ = [0 v_ 0]; %procude final matrix m = reshape(v_, length(v)+1, 2);
%set input vector v = [2 3 4]; %"double" the numbers, v_ is my temporary storage variable %all values are aligned as row vector %zeros are added at beginning and end v_ = [0, v, v, 0]; %produce final matrix m = reshape(v_, length(v)+1, 2);
I haven't checked it, since I don't have a Matlab at hand right now, but you should get the idea.
The answer by 13aumi manages this task even without the
reshape command. However, you need to pay close attention to the shape of v (row- vs- column-vector).