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

5 ビュー (過去 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 Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by