Two cells merged together with different conditions
    3 views (last 30 days)
  
       Show older comments
    
I want cell a and b merged together. 
1) When one of the two string as a value (as 5,6 or 7) I want c to have that value.
2) When the two cells has both the letter A and B, I want array c to have the letter A.
3) When the cells string has the letter A , I want array c to have the letter A.
4) When the cells string has the letter B , I want array c to have the letter B.
%Given cells
a={'A'; 'B' ; 5  ; 6; 'A' ; 'A'; 'B'};
b={ 7;  'B' ;'A' ;'B'; 'A' ; 'B' ; 'A'};
%Wanted Outcome
c={ 7;  'B' ; 5  ; 6; 'A' ; 'A'; ; 'A'};
3 Comments
Accepted Answer
  Jan
      
      
 on 19 Mar 2021
        
      Edited: Jan
      
      
 on 19 Mar 2021
  
      a = {'A'; 'B' ; 5  ; 6; 'A' ; 'A'; 'B'};
b = { 7;  'B' ;'A' ;'B'; 'A' ; 'B' ; 'A'};
c = cell(size(a));  % Pre-allocation
isNum_a     = cellfun('isclass', a, 'double');
isNum_b     = cellfun('isclass', b, 'double');
sameChar    = strcmp(a, b);  % replies FALSE, if one is a number
c(isNum_a)  = a(isNum_a);
c(isNum_b)  = b(isNum_b);    % prefer b if both are numbers  [EDITED, Typo fixed]
c(sameChar) = a(sameChar);   % or b(sameChar)
c(~sameChar & ~isNum_a & ~isNum_b) = {'A'};
6 Comments
More Answers (0)
See Also
Categories
				Find more on Data Type Identification 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!
