How to add columns to form a matrix?

2 views (last 30 days)
I am calculating a column vector in a FOR loop. I get N columns and I want to put them in a matrix.
for i = 1: N
dc(i) = ...
dcmatrix =[ dc(1) ; dc(2); .... ;dc(N) ]
%% The above example of dcmatrix is doing it manually. How can I do it with a code?%%
I calculate this dc column vector. I want to all the column vector in the form of matrix. I don't know how to do it.

Accepted Answer

Adam Danz
Adam Danz on 12 Dec 2018
Edited: Adam Danz on 12 Dec 2018
Create the matrix (or vector) from within the for-loop, not afterwards.
dcmatrix = nan(N, 1); %allocate the matrix (I don't know how many rows you need).
for i = 1:N
dcmatrix(i) = ... %On each loop, data is stored in row i

More Answers (2)

madhan ravi
madhan ravi on 12 Dec 2018
dcMatrix = dc.'

madhan ravi
madhan ravi on 12 Dec 2018
dc(i,1) % - > column vector
dc(1,i) % - > row vector
  1 Comment
Ahmad Hasnain
Ahmad Hasnain on 12 Dec 2018
Your previous answer also worked. Thanks...

Sign in to comment.




Community Treasure Hunt

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

Start Hunting!

Translated by