MATLAB Answers

convert matrix in single column

3,035 ビュー (過去 30 日間)
Gaetano Sciacovelli
Gaetano Sciacovelli 2012 年 4 月 18 日
回答済み: Kyril Kaufmann 2020 年 4 月 26 日
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 件のコメント

表示 2 件の古いコメント
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 日
yourvector = yourmatrix(:);

  5 件のコメント

表示 2 件の古いコメント
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

サインインしてコメントする。

その他の回答 (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 件のコメント

表示 4 件の古いコメント
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.

サインインしてコメントする。


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

  0 件のコメント

サインインしてコメントする。


Rifat Hossain
Rifat Hossain 2016 年 12 月 15 日
columnvector=matrix(:) this work fine

  0 件のコメント

サインインしてコメントする。


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