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

5 visualizzazioni (ultimi 30 giorni)
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.

Risposta accettata

Mischa Kim
Mischa Kim il 15 Gen 2014
Modificato: Mischa Kim il 15 Gen 2014
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 Commenti
Mischa Kim
Mischa Kim il 16 Gen 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
Venkat Ta il 3 Mag 2018
it works in between the program but this whole code does not work if I created as function

Accedi per commentare.

Più risposte (3)

Jos (10584)
Jos (10584) il 16 Gen 2014
Modificato: Jos (10584) il 16 Gen 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

Amit
Amit il 16 Gen 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

Andrei Bobrov
Andrei Bobrov il 16 Gen 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 Commenti
Charles
Charles il 4 Ago 2017
All great answers The simpler the better!!
Venkat Ta
Venkat Ta il 26 Apr 2018
Modificato: Venkat Ta il 3 Mag 2018
Thanks. It works fine.
it works in between the program but this whole code does not work if I created as function

Accedi per commentare.

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by