# convert matrix in single column

398 visualizzazioni (ultimi 30 giorni)
Gaetano Sciacovelli il 18 Apr 2012
Modificato: Image Analyst il 14 Lug 2022
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 CommentiMostra 3 commenti meno recentiNascondi 3 commenti meno recenti
Ndilokelwa Luis il 27 Ago 2018
Transpose matrix first!
Image Analyst il 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.

Accedi per commentare.

### Risposta accettata

Andrei Bobrov il 18 Apr 2012
yourvector = yourmatrix(:);
##### 10 CommentiMostra 8 commenti meno recentiNascondi 8 commenti meno recenti
rishika yadav il 14 Lug 2022
HOW WE RESHAPE THE MATRIX WITH DIFFERENT ROW NO, AND SAME COLUMBS
Image Analyst il 14 Lug 2022
Modificato: Image Analyst il 14 Lug 2022
@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.

Accedi per commentare.

### Più risposte (4)

Kyril Kaufmann il 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 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

Image Analyst il 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 CommentiMostra 5 commenti meno recentiNascondi 5 commenti meno recenti
Surya Kanthi il 25 Ott 2019
I dont know but I have a 1056x2 matrix and it does not work, any clue?
James Tursa il 25 Ott 2019

Accedi per commentare.

Rifat Hossain il 15 Dic 2016
columnvector=matrix(:) this work fine
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

AMIR KHFAGI il 23 Mar 2020
Hi, I have to convert one column vector to a matrix in matlab. How can I do this?
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Shuyun Yuan il 7 Apr 2020
reshape function

Accedi per commentare.

### Categorie

Scopri di più su Data Type Conversion in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by