Convert matrix in single column/row vector

2,012 次查看(过去 30 天)
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].

采纳的回答

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

更多回答(4 个)

M Shujah Islam Sameem
%%%% 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 个评论
Samaa Yasser
Samaa Yasser 2021-4-7
@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 2021-4-7
'the same size', do you mean a vector length 256 or 65536? In the latter case, read the answer.

请先登录,再进行评论。


Muhammad Usman
Muhammad Usman 2019-12-23
A = [1 2; 3 4];
B = A(:) % convert the matrix into a column vector
C = A(:)' % convert the matrix into a row matrix

Fariha Tabassum
Fariha Tabassum 2020-4-6
A = [1 2; 3 4];
B = A';
C = reshape(B,1,[])
ans of C will be [1 2 3 4]

Çağatay Murat Yılmaz
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 个评论
Rik
Rik 2020-10-4
Dynamically growing an array is very inefficient. You should consider transposing the array and using reshape.
Petr Hosek
Petr Hosek 2024-9-6
Technically possible, but generally the least effective solution in Matlab. I would consider this bad practice.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by