matlab array create random consecutive ones -


i create 4-dimensional array random number of consecutive ones in each row. ones should start in first column , end in random column. example:

array(:,:,1,1) = [ 1 1 1 0 0 0;                    1 1 0 0 0 0;                    1 1 1 1 1 0;                    ...         ] 

one 3 loops inefficient:

array = zeros(n,n,n,n); i= 1:n    j = 1:n       k =1:n          rows = ceil(n*rand());          array(k,1:rows,j,i) = 1;       end    end end 

can find better solution? thanks!!

simple , straight-up approach (might not random though)

rows = 8; %%// number of rows cols = 7; %%// number of columns ch3 = 3; %%// number of elements in 3rd dimension ch4 = 2; %%// number of elements in 4th dimension  array = sort(round(rand(rows,cols,ch3,ch4)),2,'descend') 

bsxfun approach (much faster shown in benchmark results below , random)

%%// sizes rows = 8; %%// number of rows cols = 7; %%// number of columns ch3 = 3; %%// number of elements in 3rd dimension ch4 = 2; %%// number of elements in 4th dimension  %%// 2d array every row starting 1 rows2 = rows*ch3*ch4; a1 = reshape(1:rows2*cols,rows2,[]); col = randi(cols,[1 rows2]); b1 = bsxfun(@plus,(col-1)*rows2,1:rows2)';%//' out = bsxfun(@le,a1,b1);  %%// rearrange 4d a2 = reshape(out',[rows*cols ch3 ch4]);%//' a3 = reshape(a2,cols,rows,ch3,ch4); array = permute(a3,[2 1 3 4]); 

benchmark results

we comparing above mentioned 2 approaches along rody's approach.

datasize i: rows = 80;  %// number of rows cols = 70;  %// number of columns ch3  = 30;  %// number of elements in 3rd dimension ch4  = 2; %// number of elements in 4th dimension  results: elapsed time sort approach is:   0.0083445sec elapsed time bsxfun approach is: 0.0021sec elapsed time rody approach is:   0.0063026sec  datasize ii: rows = 80;  %// number of rows cols = 70;  %// number of columns ch3  = 30;  %// number of elements in 3rd dimension ch4  = 20; %// number of elements in 4th dimension  results: elapsed time sort approach is:   0.07875sec elapsed time bsxfun approach is: 0.012329sec elapsed time rody approach is:   0.055937sec   datasize iii: rows = 800;  %// number of rows cols = 70;  %// number of columns ch3  = 30;  %// number of elements in 3rd dimension ch4  = 20; %// number of elements in 4th dimension  results: elapsed time sort approach is:   0.87257sec elapsed time bsxfun approach is: 0.17624sec elapsed time rody approach is:   0.57786sec   datasize iv: rows = 800;  %// number of rows cols = 140;  %// number of columns ch3  = 30;  %// number of elements in 3rd dimension ch4  = 20; %// number of elements in 4th dimension  results: elapsed time sort approach is:   1.8508sec elapsed time bsxfun approach is: 0.35349sec elapsed time rody approach is:   0.71918sec 

in conclusion these findings, bsxfun looks way go, unless want process billions of elements, memory-benchmark results produced rody's solution suggests.


Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -