How to compare two arrays of strings?
    31 views (last 30 days)
  
       Show older comments
    
    Luis Angel Manriquez Ramirez
 on 15 Nov 2022
  
    
    
    
    
    Commented: Stephen23
      
      
 on 28 Nov 2022
            Hi everyone! Currently I'm creating an with Appdesigner and I would like to compare some arrays of strings as follows:
Let's say I have two arrays:
A=['red','purple','blue','green'];
B=['orange','purple','yellow','green'];
I want to compare each element in array 'B' with array 'A'. If the element in B is diferent from A I want to save that element in a new array C and use a counter to count every element that is diferent from A.
This is what I would get after the comparison:
C=['orange','yellow']
counter=2
I hope someone can help. thanks for reading!
1 Comment
  Stephen23
      
      
 on 28 Nov 2022
				A=["red","purple","blue","green"];
B=["orange","purple","yellow","green"];
C = setdiff(B,A)
Accepted Answer
  David Hill
      
      
 on 15 Nov 2022
        Use string arrays instead of character arrays.
A=["red","purple","blue","green"];
B=["orange","purple","yellow","green"];
C=B(~ismember(B,A))
Look at what a charachter array looks like
A=['red','purple','blue','green']%it is all bunched together, the commas are meaningless
More Answers (0)
See Also
Categories
				Find more on Characters and Strings in Help Center and File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!