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. Thanks
patient profile
1开发者_C百科 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
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.
you can use accumarray
with a custom function
profile = accumarray( {PP(:,1) }, PP(:,2), [], @(x) {x} );
精彩评论