Convert matrix in single column/row vector

1 745 vues (au cours des 30 derniers jours)
Ali Baig
Ali Baig le 5 Nov 2017
Commenté : Petr le 6 Sep 2024
Hi, I have to convert a matrix in one column/row vector composed of all the rows of the original matrix. How can I do this? Thanks. For example, to convert [1 2; 3 4] in to [1 2 3 4].

Réponse acceptée

Jos (10584)
Jos (10584) le 5 Nov 2017
Take a look at reshape and transpose
A = [1 2 ; 3 4]
reshape(A,1,[])
transpose(A)
A.'
A(:)
reshape(A.',1,[])

Plus de réponses (4)

M Shujah Islam Sameem
M Shujah Islam Sameem le 5 Jan 2019
%%%% Converting Matix to vector
A = [1 2 3; 4 5 6; 7 8 9] % Example matrix
reshape(A,[],1) % convert matrix to column vector
reshape(A,1,[]) % convert matrix to row vector
  2 commentaires
Samaa Yasser
Samaa Yasser le 7 Avr 2021
@M Shujah Islam Sameem excuse me ,, i want to convert image matrix size 256x256 to row vector with length same size can you please help me ?
Rik
Rik le 7 Avr 2021
'the same size', do you mean a vector length 256 or 65536? In the latter case, read the answer.

Connectez-vous pour commenter.


Muhammad Usman
Muhammad Usman le 23 Déc 2019
A = [1 2; 3 4];
B = A(:) % convert the matrix into a column vector
C = A(:)' % convert the matrix into a row matrix
  2 commentaires
Paolo Mulazzani
Paolo Mulazzani le 8 Mar 2020
not work: instead of 1 2 3 4 the result is 1 3 2 4
Muhammad Usman
Muhammad Usman le 9 Mar 2020
A=[1 2; 3 4];
A=A';
A(:)

Connectez-vous pour commenter.


Fariha Tabassum
Fariha Tabassum le 6 Avr 2020
A = [1 2; 3 4];
B = A';
C = reshape(B,1,[])
ans of C will be [1 2 3 4]
  2 commentaires
Yezi Kadhim
Yezi Kadhim le 9 Mai 2021
Exactly what I wanted!.
A million Thanks!
rishav baishya
rishav baishya le 26 Jan 2022
thanks a lot

Connectez-vous pour commenter.


Çağatay Murat Yılmaz
Çağatay Murat Yılmaz le 4 Oct 2020
You can convert the following matrix to a vector using the following code.
input matrix:
0 1 0 2 3
4 5 6 7 8
9 10 11 12 13
output vector:
0 1 0 2 3 4 5 6 7 8 9 10 11 12 13
code:
vector=[];
for i=1:size(matrix,1)
vector=[vector matrix(i,:)];
end
  2 commentaires
Rik
Rik le 4 Oct 2020
Dynamically growing an array is very inefficient. You should consider transposing the array and using reshape.
Petr
Petr le 6 Sep 2024
Technically possible, but generally the least effective solution in Matlab. I would consider this bad practice.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by