Creating a subset of a dataset array based on values in one of the variables

15 vues (au cours des 30 derniers jours)
Andrew
Andrew le 4 Déc 2012
Suppose I have a dataset array 8 x 3 arranged as such:
Hospital Floor Beds
A R3 10
A T7 10
A Z5 8
B Q1 9
B Q8 23
B G2 13
C I8 13
C D5 15
What I want is a subset of this dataset array in which Hospital Floor is B, as shown below:
Hospital Floor Beds
B Q1 9
B Q8 23
B G2 13
Any ideas? Thanks!

Réponse acceptée

owr
owr le 4 Déc 2012
Similar to per isakson's suggestion, but with proper indexing on the "Hospital" column:
>> subset = your_ds_array( strcmp( 'B', your_ds_array.Hospital ), : );
Or, if you want to eliminate the strcmp which can be slow if your array is large and you do alot of these queries, convert the hospital column to a nominal array first:
>> your_ds_array.Hospital = nominal(your_ds_array.Hospital);
>> subset = your_ds_array( your_ds_array.Hospital == 'B', : );

Plus de réponses (0)

Catégories

En savoir plus sur Data Type Conversion dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by