MATLAB: Create a matrix with existing arrays

arraymatrix

Hi all,
I have a simple question about creating a matrix with existing arrays. Who can please help me with that:
I have 10 arrays named x1, x2,… x10. Each is of dimension 10 by 1. Now I want to put them into a single matrix, say matrix X, which is for sure of dimension 10 by 10.
I first create X=zeros(10);, and run a loop for i=1:10 X(:,i)=x(i); end but does not work, I know there is a mistake on my expression of x(i), who can please help me to fix that?
Many thanks,
Heng

Best Answer

You need to specify them individually. This is likely the only use of eval I find to be good programming:
x1 = rand(10,1);
x2 = rand(10,1);
x3 = rand(10,1);
for k1 = 1:3;
X(:,k1) = eval(sprintf('x%d',k1));
end
Change the loop limits to 10 from 3 for your code. (I didn’t feel like typing out 10 variables.)