" ":
rows = 80; %// Number of rows
cols = 70; %// Number of columns
ch3 = 30; %// Number of elements in the 3rd dimension
ch4 = 200; %// Number of elements in the 4th dimension
%// number of 1 in each row. Select the appropriate class to make
%// sure peak memory remains acceptably small
intClasses = {'uint8' 'uint16' 'uint32' 'uint64'};
maxSizes = cellfun(@(x) double(intmax(x)), intClasses);
numOnes = randi(cols, rows*ch3*ch4,1, ...
intClasses{find(cols <= maxSizes, 1,'first')});
clear intClasses maxSizes
%// Loop through all rows and flip the appropriate number of bits
array = false(rows*ch3*ch4, cols);
for ii = 1:numel(numOnes)
array(ii,1:numOnes(ii)) = true; end
clear ii numOnes
%// Reshape into desired dimensions
array = reshape(array.', cols,rows,ch3,ch4);
array = permute(array, [2 1 3 4]);
clear rows cols ch3 ch4
( sparse, MATLAB sparse double... , . : t24 > 1 0, . , -, subsref , , sparse :)
, Divakar, . , , , Divakar, .
EDIT: Divakar bsxfun , O (N²). I.e., ,
`(cols-1)·rows²·ch3²·ch4² + (cols+1)·rows·ch3·ch4
( ) 8- double , cols·rows·ch3·ch4 1- ^ _ ^
: , Divakar , profile -memory:
Divakar:

:

, (~ 130 ), , Divakar 10 × (~ 1,4 ).