How to combine random size arrays in one matrix - in a loop?

3 次查看(过去 30 天)
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.

采纳的回答

Mischa Kim
Mischa Kim 2014-1-15
编辑:Mischa Kim 2014-1-15
Hello Jeroen,
  • 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:
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 个评论
Mischa Kim
Mischa Kim 2014-1-16
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
Venkat Ta 2018-5-3
it works in between the program but this whole code does not work if I created as function

请先登录,再进行评论。

更多回答(3 个)

Jos (10584)
Jos (10584) 2014-1-16
编辑:Jos (10584) 2014-1-16
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

Amit
Amit 2014-1-16
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

Andrei Bobrov
Andrei Bobrov 2014-1-16
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 个评论
Venkat Ta
Venkat Ta 2018-4-26
编辑:Venkat Ta 2018-5-3
Thanks. It works fine.
it works in between the program but this whole code does not work if I created as function

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by