Prev: problem with while and if
Next: while loop
From: Roberto on 20 Jul 2010 09:19 I have two variables (let's say A and B). A = [99;201;333;394;501] *(5x1 double) B = {'333';'501';'629'}; *(3x1 cell) I need to know if the values in A are also in B and use that in an if/then/else statement. Something like this: if (%/a value in A is also in B\) 'do this' else (%/which means that a value in A is not in B\) 'do that' end I've tried a lot with findstr, compare, num2cell, char, etc etc etc. But I didn't find the right way... Could anyone fix it for me? TNX a lot!
From: Andy on 20 Jul 2010 09:29 "Roberto " <ivogrunn(a)hotmail.com> wrote in message <i247o8$ecm$1(a)fred.mathworks.com>... > I have two variables (let's say A and B). > A = [99;201;333;394;501] *(5x1 double) > B = {'333';'501';'629'}; *(3x1 cell) > > I need to know if the values in A are also in B and use that in an if/then/else statement. Something like this: > if (%/a value in A is also in B\) > 'do this' > else (%/which means that a value in A is not in B\) > 'do that' > end > > I've tried a lot with findstr, compare, num2cell, char, etc etc etc. But I didn't find the right way... > Could anyone fix it for me? > > TNX a lot! > > This question is not well posed. Are you iterating over the elements of A? If not, do you want to return true if any element of A is also in B, or if all elements of A are also in B, or if a certain element in A is also in B? For the purposes of looking for an element in B, do you care that it might be a number in A and a string in B?
From: Roberto on 20 Jul 2010 09:57 "Andy " <myfakeemailaddress(a)gmail.com> wrote in message <i248bi$n9m$1(a)fred.mathworks.com>... > "Roberto " <ivogrunn(a)hotmail.com> wrote in message <i247o8$ecm$1(a)fred.mathworks.com>... > > I have two variables (let's say A and B). > > A = [99;201;333;394;501] *(5x1 double) > > B = {'333';'501';'629'}; *(3x1 cell) > > > > I need to know if the values in A are also in B and use that in an if/then/else statement. Something like this: > > if (%/a value in A is also in B\) > > 'do this' > > else (%/which means that a value in A is not in B\) > > 'do that' > > end > > > > I've tried a lot with findstr, compare, num2cell, char, etc etc etc. But I didn't find the right way... > > Could anyone fix it for me? > > > > TNX a lot! > > > > > > This question is not well posed. Are you iterating over the elements of A? If not, do you want to return true if any element of A is also in B, or if all elements of A are also in B, or if a certain element in A is also in B? For the purposes of looking for an element in B, do you care that it might be a number in A and a string in B? Right, my matlab-skills aren't that good, so I hope my answer will be clear enough (otherwise: give me a call!). For all elements in A that are also in B, I have to do the same calculation (I don't think it's necessary to explain that calculation right here). And for all elements in A that aren't in B, I have to do another calculation. When I've done that, my simulation continues and (because of a while loop), variables A and B are renewed and I have to do the check them again. I think the best outcome should be two variables; one with the elements that are in A and B, and one variable with the elements that are in A only. For further calculations it would be preferable that the two variables are double. Extra info: the variables A and B contain almost 500 elements each, so speed is an important factor ;) Hopefully it's clear now...
From: Andy on 20 Jul 2010 10:09 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.
From: Roberto on 20 Jul 2010 10:19
"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 |