Merging several row vectors in a single matrix

16 次查看(过去 30 天)
Hey everybody, I am new to Matlab so my question may seem naive. I have a series of rows vectors A1, A2, A3.....An that I want to merge in a single matrix. I don't want to input them individually , that would be too long Is there a FOR-LOOP that I could use to do that? something that would look like:
Matrix= zeros(n);
for i=1:n % n is the number of vectors
statement... % a code to identify A(i)
Matrix(:,i)=A(i); % Put the vector A(n) in the i-th column of the matrix
end
I think that my main problem is that I don't know how to index A1, A2, A3...An in the for-loop.
Thanks for your help.

采纳的回答

Ahmet Cecen
Ahmet Cecen 2016-12-21
Naming your vectors A1,A2,A3 ... then assembling them like this is very inefficient. You want to either load them directly into the matrix (wherever you are getting these variables from) or use a cell array to store them as A{1}, A{2} ... instead. However, I will write down an answer that will work for this case, simply to solve your problem in the short term:
for i=1:n % n is the number of vectors
eval(["Matrix(:,i)=A",num2str(i),";"]; % will evalute the expression - Matrix(:,i)=Ai;
end
  3 个评论
Cole
Cole 2016-12-22
It worked! Thank you very much Ahmet and James.
manvi kaushik
manvi kaushik 2020-1-31
This answer is giving the transpose of the matrix. How can i correct it?

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by