MATLAB Answers

convert matrix in single column

조회 수: 3,435(최근 30일)
Gaetano Sciacovelli
Gaetano Sciacovelli 2012년 4월 18일
댓글: Paúl Aguilar 2021년 9월 16일
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
Image Analyst
Image Analyst 2020년 4월 9일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Andrei Bobrov
Andrei Bobrov 2012년 4월 18일
yourvector = yourmatrix(:);
  댓글 수: 8
Paúl Aguilar
Paúl Aguilar 2021년 9월 16일
Excelente, thanks sir.

댓글을 달려면 로그인하십시오.

추가 답변(4개)

Image Analyst
Image Analyst 2012년 4월 18일
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
James Tursa
James Tursa 2019년 10월 25일
Please post a new Question with the details of your problem.

댓글을 달려면 로그인하십시오.


Kyril Kaufmann
Kyril Kaufmann 2020년 4월 26일
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

Rifat Hossain
Rifat Hossain 2016년 12월 15일
columnvector=matrix(:) this work fine

AMIR KHFAGI
AMIR KHFAGI 2020년 3월 23일
Hi, I have to convert one column vector to a matrix in matlab. How can I do this?
  댓글 수: 1
Shuyun Yuan
Shuyun Yuan 2020년 4월 7일
reshape function

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by