ismember input - strings vs numbers

Hello,
I am using "ismember" to pullout data structured as a dataset.
  • I am able to do both of these commands:
ismember(data.dataTag,0:24);
ismember(data.dataTag,{'A' 'B'});
  • I would like to automate this process by feeding "ismember" a blanket series of criterion and run through all my "dataTag" of my data. I however run into trouble when I do this:
ismember(data.(dataTag{n}),{{'A' 'B'} 0:24});
ismember(data.(dataTag{n}),[{'A' 'B'} 0:24]);
The error I get is "Input must be cell arrays of strings."
Can this be done? Thank you for any help.
Matthieu

Answers (3)

Hi Matthieu,
Are the criterion pre-defined? If so, how about concatenating them with logical operator, e.g.,
ismember((data.(dataTag{n}),{'A' 'B'}) || ismember((data.datqaTag{n}),0:24)
Hello Honglei,
Thank you for the quick answer!
The function that i would like to use would be along these lines:
function c = classifyData(data,criterion)
% data is the data - Type datset
% criterion is the selection criterion for the sorting of the data - Type anything
classifiers = {'dataTag1' 'dataTag2' 'dataTag3'};
c = [];
for n = 1:length(classifiers)
c = [c;ismember(data.(classifiers{n}),criterion)'];
end
c = (sum(c) == max(sum(c)))';
end
I would like to feed "criterion" without having to split it like you suggested i.e:
criterion = {{'A' 'B'} 0:24};
Otherwise your answer would work.
Thank you for you input. Any thought?
Matthieu
Honglei Chen
Honglei Chen on 6 Sep 2011
Hi Matthieu,
I think ismember(A,B) requires A and B to be the same data type. One possible solution is to use num2str to build criterion so that criterion are all strings. Then in your function, you can use isnumeric or ischar to test the data. If the data is a number, you can do num2str again before you call ismember. However, you'll have to convert it back to number for your computation. I'm not sure if it's worthy though.
BTW, I'm also confused by you code snippet. If you need to do something like sum(c), then can c be strings? I know sum will work on strings but is that what you want?
HTH

Categories

Asked:

on 6 Sep 2011

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!