- Generate the vectors - I'll call them b - in your for-loop and keep track of the longest vector that is generated in the loop. Store the vectors as a matrix: b(:,ii).
- Once done, initialize a 0-matrix A using zeros(M,N) where M is equal to the size of the longest vector and N is the total number of vectors.
- Lastly run another for-loop to paste into the matrix the individual vectors using something like:
How to combine random size arrays in one matrix - in a loop?
4 views (last 30 days)
Show older comments
Hey, I've been able to find a lot of solutions for my matlab program on this site but the next problem still goes unanswered.
I have a loop in which every run a vector is created with a random size. After the loop is finished I want all these vectors presented in a matrix with each vector as a column. How can I do this? The shorter vectors may either be filled with NaN's or zero's.
In essence the program looks like this:
for i=1:1:10;
l=ceil(rand(1)*10);
vector=rand(l,1);
end
matrix=[vector1 vector2 ... vector10 ];
Thanks in advance for helping me out.
0 Comments
Accepted Answer
Mischa Kim
on 15 Jan 2014
Edited: Mischa Kim
on 15 Jan 2014
Hello Jeroen,
A(:,ii) = [b(:,ii); zeros(length(A(1,:)) - length(b(:,ii)),1)]
where ii is the running index of the loop.
You can also get this done using only one loop, of course, by simply concatenating vectors that have been "filled up" with the adequate number of 0s, or filling up the existing matrix before concatenating.
2 Comments
Mischa Kim
on 16 Jan 2014
Run the code below. Not optimized but it does what you are looking for:
A = [0];
for ii = 1:10
b = ones(randi([1,10],1,1), 1); % here you put your random-sized vector
if (size(A(:,1)) == 1)
A = b;
else
if (length(b) < length(A(:,1)))
A = [A [b; zeros(length(A(:,1)) - length(b),1)]];
end
if (length(b) == length(A(:,1)))
A = [A b];
end
if (length(b) > length(A(:,1)))
A = [[A; zeros(length(b) - length(A(:,1)), length(A(1,:)))] b];
end
end
end
display(A);
Venkat Ta
on 3 May 2018
it works in between the program but this whole code does not work if I created as function
More Answers (3)
Jos (10584)
on 16 Jan 2014
Edited: Jos (10584)
on 16 Jan 2014
You have several options how to store vectors with different lengths.
1) use cell arrays
C = cell(k,1) ;
for k=1:10,
L = ceil(10*rand) ;
C{k} = rand(L,1) ;
end
2) subsequently you can concatenate these cells into a single array, in which shorter vectors are padded with a chosen value. I have submitted the function PADCAT to the file exchange, which makes this a trivial conversion:
[M, tf] = padcat(C{:}) % pad with NaNs by default
M(~tf) = 0 % change NaNs to zeros
0 Comments
Amit
on 16 Jan 2014
how about:
matrix = zeros(10,10);
flag = 1;
for i = 1:10
m = randi(10);
matrix(1:m,flag:flag+m-1) = rand(m);
flag = flag + m;
end
0 Comments
Andrei Bobrov
on 16 Jan 2014
for jj = 1:10
v = randi(randi(234),randi(12),1);
if jj==1
out = v;
else
n = [size(out,1),numel(v)];
out(n(1)+1:n(2),1:jj-1) = nan;
out(1:max(n),jj) = nan;
out(1:n(2),jj) = v;
end
end
2 Comments
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!