I have a cell array with 943 cells, each cell contains an array of binary elements. I want to apply a function (e.g. 'and' operation) on each pair of cells, for example:
and(cell1,cell2), and(cell1,cell3) ..... and(cell1,cell943)
and(cell2,cell3) ..... and(cell2,cell943)
. .
. .
. .
. and(cell942,cell943)
For the efficiency purpose, i don't want to repeat the function on a same pair twice. How can i do this?
This would be a solution using a simple for loop:
A = { [0 1 0 1 0 1 0 1] ;
[1 1 1 0 1 0 0 1] ;
[0 0 0 1 1 1 0 1] }
n = numel(A);
combs = nchoosek(1:n,2)
for ii = 1:n
output{ii,1} = A{combs(ii,1)} & A{combs(ii,2)};
output{ii,2} = combs(ii,:);
end
returning:
In the first column you have the result of your operation and in the second column the rows involved (of the initial cell array).
Or use arrayfun
instead of the loop:
output = arrayfun(@(x) A{combs(x,1)} & A{combs(x,2)},1:n,'uni',0).';