Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: subset of dataset
Replies: 3   Last Post: Dec 21, 2012 4:15 AM

 Messages: [ Previous | Next ]
 dpb Posts: 9,850 Registered: 6/7/07
Re: subset of dataset
Posted: Dec 20, 2012 2:03 PM

On 12/20/2012 9:26 AM, Barry Shaw wrote:
...

> If I want to pick out those patients Age = 38 I can:
>>> subset_Age38 = hospital(hospital.Age == 38,:)
>
> If I want to pick out those patients Age = 39 I can:

>>> subset_Age39 = hospital(hospital.Age == 39,:)
>
> If I want to pick out those patients Age = 38 AND 39 I try:
> subset_Age3839 = hospital(hospital.Age == [38 39],:)
>
> but get the error
> Error using == Matrix dimensions must agree.

Correct...the syntax for that would be

subset_Age3839 = hospital(hospital.Age==38|hospital.Age==39,:)

> I can use the following work around, which gives me what I want:
>>> ages_to_view = [38 39];
>>> [refs] = find(ismember(hospital.Age,ages_to_view));
>>> subset_Age3839 = hospital(refs,:)

>
> But is there a way to do this as I initially tried it? I don't want to
> use a 38 | 40, as I may have an awful lots of values to set as criteria.

...

It's what ismember() and friends are for--to reduce the tedium of
individual conditional statements.

--

Date Subject Author
12/20/12 Barry Shaw
12/20/12 dpb
12/20/12 dpb
12/21/12 Barry Shaw