MATLAB Answers

# convert matrix in single column

3,183 views (last 30 days)
Gaetano Sciacovelli on 18 Apr 2012
Commented: Farid on 6 Apr 2021 at 20:19
Hi, I have to convert a matrix in one column vector composed of all the columns of the original matrix. How can I do this? Thanks
##### 5 CommentsShowHide 4 older comments
Image Analyst on 9 Apr 2020
You said "I have to convert a matrix in one column vector composed of all the columns of the original matrix." I thought you meant you had a column vector and had to convert it to a matrix having the same number of columns as the original matrix from where the column vector came. In other words, I thought you meant "I have to convert a matrix of one column vector INTO ONE composed of all the columns of the original matrix."
Seeing the answer you accepted, it appears that you actually meant "I have to convert a matrix INTO a one column vector that is composed of all the columns of the original matrix." Leaving out seemingly minor words completely changes the interpretation of the question, as does their placement in the sentence.

Sign in to comment.

### Accepted Answer

Andrei Bobrov on 18 Apr 2012
yourvector = yourmatrix(:);
##### 6 CommentsShowHide 5 older comments
Farid on 6 Apr 2021 at 20:19
WOW

Sign in to comment.

### More Answers (4)

Image Analyst on 18 Apr 2012
If your column vector was "composed of all the columns of the original matrix", then use the reshape() command to turn it from a column vector back into the original 2D matrix.
matrix2D = reshape(columnVector, [rows columns]);
(The converse, how to get the column vector in the first place (what you may have done to get your vector) is accomplished like this columnVector = fullMatrix(:).)
##### 7 CommentsShowHide 6 older comments
James Tursa on 25 Oct 2019
Please post a new Question with the details of your problem.

Sign in to comment.

Kyril Kaufmann on 26 Apr 2020
For a more algorithmic solution:
% From matrix to vector
N = 10;
mat1 = rand(N);
vec1 = zeros(N*N,1);
for i=1:N
for j=1:N
vec1((i-1)*N + j) = mat1(i,j);
end
end
% From vector to matrix
N = 10;
vec2 = rand(N*N,1);
mat2 = zeros(N);
for i=1:N
for j=1:N
mat2(i,j) = vec2((i-1)*N + j);
end
end
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

Rifat Hossain on 15 Dec 2016
columnvector=matrix(:) this work fine
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

AMIR KHFAGI on 23 Mar 2020
Hi, I have to convert one column vector to a matrix in matlab. How can I do this?
##### 1 CommentShowHide None
Shuyun Yuan on 7 Apr 2020
reshape function

Sign in to comment.

### Community Treasure Hunt

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

Start Hunting!