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

조회 수: 7 (최근 30일)
Jeroen
Jeroen 2014년 1월 15일
편집: Venkat Ta 2018년 5월 3일
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
Charles
Charles 2017년 8월 4일
All great answers The simpler the better!!
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