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

Sazzad Sayyed
Sazzad Sayyed 2016년 2월 9일
You can try matA=matA(:).This works.
muhammad komugabe
muhammad komugabe 2017년 10월 18일
great. thanx
Luke Handscomb
Luke Handscomb 2018년 4월 6일
This takes column1 and then appends column2 to the bottom of 1 and 3 to 2 and so on. What if I wanted to instead arrange it as row1+row2+row3....? Cheers
Ndilokelwa Luis
Ndilokelwa Luis 2018년 8월 27일
Transpose matrix first!
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일

72 개 추천

yourvector = yourmatrix(:);

댓글 수: 10

muhammad komugabe
muhammad komugabe 2017년 10월 18일
wow. it works fine
sayyam khurana
sayyam khurana 2017년 11월 20일
NOT ALL HEROS WEAR CAPES.
Akash Singh
Akash Singh 2018년 12월 12일
I know it works fine, but how?
James Tursa
James Tursa 2018년 12월 12일
Because (:) is the syntax in MATLAB for turning a variable into a column.
Mihai Gabriel Calitescu
Mihai Gabriel Calitescu 2020년 4월 8일
A man with this much power in his bare hands... im scared
Farid
Farid 2021년 4월 6일
WOW
Pablo Moreno Galbis
Pablo Moreno Galbis 2021년 8월 19일
Drops the mic.
Paúl Aguilar
Paúl Aguilar 2021년 9월 16일
Excelente, thanks sir.
rishika yadav
rishika yadav 2022년 7월 14일
HOW WE RESHAPE THE MATRIX WITH DIFFERENT ROW NO, AND SAME COLUMBS
Image Analyst
Image Analyst 2022년 7월 14일
편집: Image Analyst 2022년 7월 14일
@rishika yadav you can use interp2 to interpolate a different height:
m = reshape(1:18, [], 3) % Create 6 row by 3 column sample data
[oldHeight, columns] = size(m)
% Make the matrix taller by interpolating.
newHeight = 8;
[xq,yq] = meshgrid(1:columns, linspace(1, oldHeight, newHeight));
mTaller = interp2(m, xq, yq)
fprintf('The size of mTaller is %d rows by %d columns.\n\n', size(mTaller, 1), size(mTaller, 2))
% Make the matrix taller by interpolating.
newHeight = 3;
[xq,yq] = meshgrid(1:columns, linspace(1, oldHeight, newHeight));
mShorter = interp2(m, xq, yq)
fprintf('The size of mShorter is %d rows by %d columns.\n', size(mShorter, 1), size(mShorter, 2))
The first/top and last/bottom rows will have the same values, and more, or fewer, rows will be interpolated in between the top row and bottom row so that you have your new desired height.

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

추가 답변 (4개)

Kyril Kaufmann
Kyril Kaufmann 2020년 4월 26일

4 개 추천

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
Image Analyst
Image Analyst 2012년 4월 18일

3 개 추천

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

yang liu
yang liu 2018년 3월 31일
hey, I want to know which is faster? command 'reshape()' or '(:)', are they two do the job based on the same underlying code? Thanks, I'm try to get my code running faster.
Image Analyst
Image Analyst 2018년 3월 31일
I would guess that (:) is faster, but they're going in opposite directions. Just use tic and toc a bunch of times to test it and see.
Akash Singh
Akash Singh 2018년 12월 12일
How is (:) working? I'm trying to understand the steps behind this method.
James Tursa
James Tursa 2018년 12월 12일
편집: James Tursa 2018년 12월 12일
This has already been answered. The reason (:) turns a variable into a column is because MATLAB is programmed that way. That's what this particular syntax does. No other reason. There are no "steps" behind it. It is the equivalent of reshape(your_variable,numel(your_variable),1);
Wolfgang Klassen
Wolfgang Klassen 2019년 7월 31일
Matlab has multiple kinds of indexing, and which one gets used is often a function of how many indices you use. A(2,3) accesses the element in the second row, third column. A(6) accesses the sixth element in the matrix, starting numbering in the first column and going down the columns until you get to the end. Just like you might say A(1,:) accesses all the columns in the first row, A(:) accesses all the elements in that ordering scheme, which happens to be all the elements in the matrix, in a particular order. If you wanted a different order, you'd have to use reshape, or maybe transpose it first.
Surya Kanthi
Surya Kanthi 2019년 10월 25일
I dont know but I have a 1056x2 matrix and it does not work, any clue?
James Tursa
James Tursa 2019년 10월 25일
Please post a new Question with the details of your problem.

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

Rifat Hossain
Rifat Hossain 2016년 12월 15일

0 개 추천

columnvector=matrix(:) this work fine
AMIR KHFAGI
AMIR KHFAGI 2020년 3월 23일

0 개 추천

Hi, I have to convert one column vector to a matrix in matlab. How can I do this?

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by