필터 지우기
필터 지우기

Searching values in a dataset

조회 수: 8 (최근 30일)
Mark
Mark 2012년 2월 27일
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