Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply function to every pair of cells in a cell array

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?

like image 551
faezeh Avatar asked Dec 09 '25 19:12

faezeh


1 Answers

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:

enter image description here

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).';
like image 126
Robert Seifert Avatar answered Dec 11 '25 08:12

Robert Seifert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!