convert matrix in single column
Ältere Kommentare anzeigen
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 Kommentare
Sazzad Sayyed
am 9 Feb. 2016
You can try matA=matA(:).This works.
muhammad komugabe
am 18 Okt. 2017
great. thanx
Luke Handscomb
am 6 Apr. 2018
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
am 27 Aug. 2018
Transpose matrix first!
Image Analyst
am 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.
Akzeptierte Antwort
Weitere Antworten (4)
Kyril Kaufmann
am 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
Image Analyst
am 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 Kommentare
yang liu
am 31 Mär. 2018
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
am 31 Mär. 2018
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
am 12 Dez. 2018
How is (:) working? I'm trying to understand the steps behind this method.
James Tursa
am 12 Dez. 2018
Bearbeitet: James Tursa
am 12 Dez. 2018
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
am 31 Jul. 2019
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
am 25 Okt. 2019
I dont know but I have a 1056x2 matrix and it does not work, any clue?
James Tursa
am 25 Okt. 2019
Please post a new Question with the details of your problem.
Rifat Hossain
am 15 Dez. 2016
0 Stimmen
columnvector=matrix(:) this work fine
AMIR KHFAGI
am 23 Mär. 2020
0 Stimmen
Hi, I have to convert one column vector to a matrix in matlab. How can I do this?
1 Kommentar
Shuyun Yuan
am 7 Apr. 2020
reshape function
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!