Search code examples
arraysmatlabindexingcellcell-array

MatLab find cells with specific values in two cell arrays


I want to find cells, which are at the same position in two different cell arrays and which have specific values.

The two cell arrays have the following structure:

cell array C1= cell(20,1). In each cell there is another cell cell(8129,8) holding double values in the range of [0,1].

Cell array C2= cell(20,1). In each cell there is another cell cell(8192,8) also holding double values in the range of [0,1].

I know want to find those cells, which (1) have a specific value that I determine (e.g. C1_value = 0.8 and C2_value = 0.85) and (2) are at the same position in the respective sub cell (!) array (e.g. C1{2}(736) == 0.8 and C2(19)(736) == 0.85). NOTE: The same position only refers to the subcell arrays (cell(8192,8)) not the "main" cell arrays C1(:) and C2(:)

Thanks in advance!


Solution

  • See if this approach works for you -

    sz = cell2mat(cellfun(@size,C1(1),'uni',0))
    row1 = sz(1);
    col1 = sz(2);
    
    t1 = reshape(horzcat(C1{:}),row1,col1,[])
    t2 = reshape(horzcat(C2{:}),row1,col1,[])
    
    b1 = t1==C1_value
    b2 = t2==C2_value
    
    tt1 = reshape(b1,row1*col1,[])' %//'
    tt2 = reshape(b2,row1*col1,[])' %//'
    
    tt22 = permute(tt2,[3 2 1])
    tt3 = bsxfun(@and,tt1,tt22)
    [C1_cellnum,subcellnum,C2_cellnum] = ind2sub(size(tt3),find(tt3)) %// outputs
    

    Thus, with your sample data, you must have -

    C1_cellnum as 2, C2_cellnum as 19 and subcellnum as 736.