5692

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

Question:

I created 1D array which shows words and in which sentences they occur. After that I took the intersection to show which word occurs with which each of other remaining words in sentence:

OccursTogether = cell(length(Out1)); for ii=1:length(Out1) for jj=ii+1:length(Out1) OccursTogether{ii,jj} = intersect(Out1{ii},Out1{jj}); end end celldisp(OccursTogether)

the output of above code is as 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 to check a single element if its deletion doesn't cause empty set it should be deleted but if its deletion give rise to empty set it should not be deleted.

for example:

step1:Delete 4 from {1,1}and{1,5} it will not be empty so 4 should be deleted.

step2: Delete 14 from {1,1}and{1,4} it will not be empty so 14 should also be deleted.

step3: If i delete 11 from {1,1}and{1,5} it will cause empty set because 4 and 14 are deleted in step 1 and step 2 so it should not be deleted.

Element deletion operations should be carried out for all the cells of the arrays.OccursTogether is declared as 1D cell array.

How can i code to make Comparisons and deletions for all OccursTogether cell array locations?

Answer1:

Use the following, where C stands for your OccursTogether cell (shorter, thus easier to read for this answer). The comments in the code explain a bit what the 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 from elements of C numsInCell = unique([C{:}]); % numbers in cell, sorted for n = numsInCell % loop over numbers in cell lSetIs1 = cellfun(@numel,C) == 1; % length of set is 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 and has length of 1, nor n is unique if condition % if false for all sets... C = cellfun(@(set) set(set~=n), C, 'UniformOutput', false); % ... then remove n from all sets end end celldisp(C)

Notice I use logical indexing in the line in the for-loop starting with C = cellfun(..., which saves you an additional for-loop over the elements of C. The MATLAB function cellfun performs the function handle in its first argument on the elements of the cell in the second argument. This is a very useful tool that prevents the use of many for-loops and even some if-statements.

Recommend

  • AMScrollingNavbar does not work. Nothing happens
  • Search and print matching string
  • Why doesn't copyTo(… PASTE_VALUES) work in the middle of a macro?
  • How to know the return address of a function?
  • Waterline (Sails.js): AND conditions
  • Regex , Detect all strings preceded with a certain character
  • Facebook Messenger Chatbot for personal fan page
  • MultiResourceItemReader with a custom delegate keeps reading the same file
  • Reference the second row in named table for data validation
  • How to pass props to react component through routing using react-router?
  • Search, Insert, Delete and DeleteLast operations in logN
  • How to abort an insert of multiple rows in a trigger
  • Javascript delete cookie before reload or redirect
  • Reusing the CQ5 Form into the mywebsite components is not showing up the End of the Form section
  • Swift - pruning elements from an Array, converting integer strings to integers
  • 'doc_del_count' bigger than 'doc_count' on CouchDB
  • How can I get the choice “H2” back in the H2 consol?
  • Upload file that is in the cpan database
  • Find group of records that match multiple values
  • How do I signal completion of my dataflow?
  • C: Incompatible pointer type initializing
  • why xml file does not aligned properly after append the string in beginning and end of the file usin
  • Linq Objects Group By & Sum
  • Adding a button at the bottom of a table view
  • Sails.js/waterline: Executing waterline queries in toJSON function of a model?
  • Getting last autonumber in access
  • Optimizing database types to compact database (SQLite)
  • Redux, normalised entities and lodash merge
  • Weird JavaScript statement, what does it mean?
  • Adding custom controls to a full screen movie
  • Do create extension work in single-user mode in postgres?
  • Delete MySQLi record without showing the id in the URL
  • GridView Sorting works once only
  • How do I rollback to a specific git commit
  • Buffer size for converting unsigned long to string
  • How to set the response of a form post action to a iframe source?
  • Change div Background jquery
  • Qt: Run a script BEFORE make
  • Django query for large number of relationships
  • reshape alternating columns in less time and using less memory