tags:

views:

399

answers:

1

Suppose i have 3 patients with their profiles but the profiles do not have the same length. My question is how to create a cell-array from this data (see below) where each entry of my cell-array will correspond to one patient-profile. Think's

patient profile

1 2

1 3

1 -2

1 1

1 3

1 2

2 -1

2 -3

2 0

2 -2

3 2

3 2

3 1

3 3

3 2

3 -1

3 -2

3 -3

3 -2

3 -2

+4  A: 

Use mat2cell as the engine. I assume that you want to aggregate the results of the second column of your patient profile array, as a function of the first column. If the profile is in an array called PP:

counts = [6;4;10];
C = mat2cell(PP(:,2),counts)
C = 
    [ 6x1 double]
    [ 4x1 double]
    [10x1 double]

See that this step has done the job of extracting the data into separate cells.

C{:}
ans =
     2
     3
    -2
     1
     3
     2
ans =
    -1
    -3
     0
    -2
ans =
     2
     2
     1
     3
     2
    -1
    -2
    -3
    -2
    -2

If PP was not sorted in advance, we would have needed to sort on the first column of PP. So all that we need to do is to know how many entries corresponded to each patient. That is a task that accumarray can handle.

counts = accumarray(PP(:,1),1)
counts =
     6
     4
    10

Finally, had the list of patient identifiers been more sophisticated than the numbers (1:3), a call to unique would have resolved that problem for you.

woodchips

related questions