matlab - How to delete element from cell arrays after comparisons without causing cell arrays to get empty? -


i created 1d array shows words , in sentences occur. after took intersection show word occurs each of other remaining words in sentence:

occurstogether = cell(length(out1)); ii=1:length(out1) jj=ii+1:length(out1) occurstogether{ii,jj} = intersect(out1{ii},out1{jj}); end end celldisp(occurstogether) 

the output of above code below:

occurstogether{1,1} =   4 11 14  occurstogether{1,2} =   1 occurstogether{1,3} =   [] occurstogether{1,4} =   1 4 8 14 15 19 20 22  occurstogether{1,5} =   4 11 

i want check single element if deletion doesn't cause empty set should deleted if deletion give rise empty set should not deleted.

for example:

step1:delete 4 {1,1}and{1,5} not empty 4 should deleted.

step2: delete 14 {1,1}and{1,4} not empty 14 should deleted.

step3: if delete 11 {1,1}and{1,5} cause empty set because 4 , 14 deleted in step 1 , step 2 should not deleted.

element deletion operations should carried out cells of arrays.occurstogether declared 1d cell array.

how can code make comparisons , deletions occurstogether cell array locations?

use following, c stands occurstogether cell (shorter, easier read answer). comments in code explain bit corresponding lines do.

c = cell(3,2);  c{1,1} = [4 11 14]; c{2,1} = 1; c{2,2} = [1 4 8 14 15 19 20 22]; c{3,2} = [4 11]; celldisp(c)  c = cellfun(@unique, c, 'uniformoutput', false); % remove duplicates elements of c  numsincell = unique([c{:}]); % numbers in cell, sorted  n = numsincell % loop on numbers in cell     lsetis1 = cellfun(@numel,c) == 1; % length of set 1     ninset = cellfun(@(set) any(set==n), c); % set contains n     nisunique = sum(ninset(:))==1; % n occurs once     condition = ~nisunique & ~any(ninset(:) & lsetis1(:)); % set neither contains n , has length of 1, nor n unique     if condition % if false sets...         c = cellfun(@(set) set(set~=n), c, 'uniformoutput', false); % ... remove n sets     end end  celldisp(c) 

notice use logical indexing in line in for-loop starting c = cellfun(..., saves additional for-loop on elements of c. matlab function cellfun performs function handle in first argument on elements of cell in second argument. useful tool prevents use of many for-loops , if-statements.


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -