function [fmasks] = removeDuplicates(masks) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %Removes duplicate ROIs within a matrix of identified neuron ROIs/masks % Inputs: % - masks: MxNxnNeuron matrix of the rois identified for a field of % view % Output: % - fmasks: MxNxnNeuron matrix of the rois identified for a field of % view with duplicates removed from the matrix %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% [sm1_1 sm1_2 sm1_3] = size(masks); for m1 = 1:sm1_3 mask1 = masks(:, :, m1); m1i{m1} = find(mask1); end dist = NaN(sm1_3, sm1_3); for m1 = 1:sm1_3 for m2 = 1:sm1_3 m1ind = m1i{m1}; m2ind = m1i{m2}; union = length(unique(cat(1, m1ind, m2ind))); int1 = intersect(m1ind, m2ind); int2 = intersect(m2ind, m1ind); dist(m1, m2) =length(int1)/union; end end udist = triu(dist,1); ind = find(udist > 0.33); [row col] = ind2sub(size(dist), ind); remind = []; for fn = 1:length(ind) c1 = length(m1i{row(fn)}); c2 = length(m1i{col(fn)}); if c1 > c2 remind(fn) = col(fn); else remind(fn) = row(fn); end end fmasks = masks; fmasks(:, :, remind) = [];