Searching values in a dataset

8 次查看(过去 30 天)
Hi
I need to apply some filters to a dataset, or just look for some specific quantity. However, I receive an error that the eq operator is not defined for datasets. I know I can convert a particular column to, e.g., double and then use the eq operator for double, but if the dataset is big, I am concerned about performance overheads due to constant conversions.
So
  • Is there any way to apply a filter to a dataset without converting it first to other type (e.g., double)?
  • Is there any way to look for values in specific columns of a dataset without converting such column first to other type (e.g., double)?
Thanks in advance

采纳的回答

Peter Perkins
Peter Perkins 2012-2-27
Quite often, this is the kind of thing you'd do to pick out rows of a dataset array:
>> load hospital
>> hospital(hospital.Age>45 & hospital.Sex=='Male',{'LastName' 'Age' 'Smoker'})
ans =
LastName Age Smoker
KOQ-996 'MARTIN' 48 true
XBA-581 'ROBINSON' 50 false
YYV-570 'SCOTT' 47 false
FLJ-908 'STEWART' 49 true
DAU-529 'REED' 50 true
FCD-425 'GONZALES' 48 false
MEZ-469 'GRIFFIN' 49 false
ZZB-405 'HAYES' 48 false
There's not really any need to explicitly "convert" Age to double, just by subscripting using dot, you get back the Age variable itself. Similarly for the Sex variable. Performance wise, this is actually (under the hood) a shared copy of something that already exists, so it should be fast.
  3 个评论
Mark
Mark 2012-2-27
I found the answer myself, but with your valuable help of course:
>> strField='Sex';
>> strValue='Male';
>> hospital( hospital.(strField) ==strValue,{'LastName' 'Age' 'Smoker'})
I didn't know about the syntax .(field name)
Peter Perkins
Peter Perkins 2012-2-28
Yes, just as with a structure array, you can use that "dot parentheses" syntax if the name is stored in a string variable.
Your initial guess, which involved
hospital(:, {strField})==strValue
illustrates a point that is sometimes hard to understand at first: a dataset array (much like a cell array) is a container, and subscripting using parentheses preserves that "containerness", and so hospital(:, {strField}) is dataset array that only contains one variable, but it is not the same thing as that variable. Much the same as with a scalar structure, it's the dot subscripting that gets you the "contents".
One more minor point: hospital(:, strField) (without the braces around strField) would have been fine, since you've only got one variable name.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by