MATLAB: Best way to seperate a very large array of numbers into different variables based on a value tat is given in the first column

help

I have a variable that contains experimental data, it is 96×300001. (however the size will change every time I run my script). The first column gives a value which represents a certain group (e.g group 1, group 2 group 3 etc. The variable kinda looks like this =
1 0.021 0.0433 0.656,
2 0.00321 0.335 0.605,
3 0.1 0.035 0.625. etc etc
I want to tell MATLAB "numbers next to the number 1 go into variable "group1" and numbers next to the number 2 go into a variable called "group2" and so on, but I have no idea how to do this!

Best Answer

Here is an example: we build a fake data set:
>> data = [sort(randi(3,10,1)), rand(10,5)]
data =
1.0000 0.9727 0.6504 0.8675 0.9329 0.2748
1.0000 0.9217 0.1238 0.1572 0.7492 0.7884
1.0000 0.7864 0.9880 0.6371 0.8526 0.8418
1.0000 0.2187 0.6194 0.9931 0.9952 0.9586
2.0000 0.1218 0.9761 0.8390 0.6048 0.2233
2.0000 0.5117 0.0724 0.9759 0.2118 0.7046
3.0000 0.6800 0.3582 0.4349 0.8793 0.8167
3.0000 0.8622 0.4866 0.7729 0.8863 0.8443
3.0000 0.6063 0.8812 0.5100 0.6926 0.9223
3.0000 0.7486 0.0681 0.4883 0.6132 0.4820
and we group it this way:
>> groups = splitapply( @(x){x(:,2:end)}, data, data(:,1) )
groups =
3×1 cell array
{4×5 double}
{2×5 double}
{4×5 double}
Now groups is a cell array that we can index using the group ID:
>> groups{1}
ans =
0.9727 0.6504 0.8675 0.9329 0.2748
0.9217 0.1238 0.1572 0.7492 0.7884
0.7864 0.9880 0.6371 0.8526 0.8418
0.2187 0.6194 0.9931 0.9952 0.9586
>> groups{2}
ans =
0.1218 0.9761 0.8390 0.6048 0.2233
0.5117 0.0724 0.9759 0.2118 0.7046
>> groups{3}
ans =
0.6800 0.3582 0.4349 0.8793 0.8167
0.8622 0.4866 0.7729 0.8863 0.8443
0.6063 0.8812 0.5100 0.6926 0.9223
0.7486 0.0681 0.4883 0.6132 0.4820