Не уверен, что предыдущий ответ на самом деле делает то, что вы хотите ... С помощью этого метода я получаю несколько записей в массиве 2D, которые являются одинаковыми. Вот vectorised и (я считаю) правильное решение:
clear all;
nRows = 2;
nCols = nRows; % Only works for square matrices
% Generate matrix of all binary numbers that fit in nCols
max2Pow = nCols;
maxNum = 2^max2Pow - 1;
allBinCols = bsxfun(@bitand, (0:maxNum)', 2.^((max2Pow-1):-1:0)) > 0;
% Get the indices of the rows in this matrix required for each output
% binary matrix
N = size(allBinCols, 1);
A = repmat({1:N}, nCols, 1);
[B{1:nCols}] = ndgrid(A{:});
rowInds = reshape(cat(3, B{:}), [], nCols)';
% Get the appropriate rows and reshape to a 3D array of right size
nMats = size(rowInds, 2);
binMats = reshape(allBinCols(rowInds(:), :)', nRows, nCols, nMats)
Обратите внимание, что для чего-нибудь, кроме небольшого числа nRows
вы будете запускать из памяти довольно быстро, потому что вы генерируете 2^(nRows*nRows)
матрицы размером nRows*nRows
. ThatsAlottaNumbers.