Prev: problem with while and if
Next: while loop
From: Andy on 20 Jul 2010 10:30 "Roberto " <ivogrunn(a)hotmail.com> wrote in message <i24b8p$6v9$1(a)fred.mathworks.com>... > "Andy " <myfakeemailaddress(a)gmail.com> wrote in message <i24am0$q1a$1(a)fred.mathworks.com>... > > But you still didn't answer an important question. You have given: > > > > A = [99;201;333;394;501]; > > B = {'333';'501';'629'}; > > > > Should the elements 333 and 501 in A match the elements '333' and '501' in B? Note: these are doubles in A, but they are strings in B. > > Yes, indeed! The outcome of the 'check' would be: > matching elements: 333 and 501 > non-matching elements: 99, 201 and 394 Okay, try this: A = [99;201;333;394;501]; B = {'333';'501';'629'}; B=cellfun(@(x) str2double(x),B); [C,IA,IB] = intersect(A,B); A(IA)=[]; B(IB)=[]; Notes: after running this, C holds the common values, A holds the values only in A, and B holds the values only in B. (If you don't want to alter A and B, make a copy of them and alter the copy.) Note that this converts B into a numeric array, and converts the strings to doubles. I don't know how slow this might be for that size input. Also, if the cell array B contains things other than strings that can be str2double'd, then cellfun might not be happy. |