1
votes

I'm pretty new to Matlab and although I could probably just do this iteratively I'm wondering what is the cleanest way using possibly built-in Matlab functionality.

I have a .mat file that contains a feature matrix X that is of size 150x4 and a class label vector Y that is of size 150x1. What is the idiomatic way in Matlab to read in and divide the matrix X into separate matrices for each class group?

2
See the grp2idx function.Andrei Davydov

2 Answers

1
votes

Use find function in Matlab. Below is an example:

index = find(Y==somevalue);
subX = X(index,:);
0
votes

So this is the best way I figured out to do it. If anybody knows a better way like some built in function that does all of this, that'd be even better.

[row_size feature_size] = size(X_train);
classes = unique(Y_train);
grouped_data = cell(length(classes),1);

for i=1:length(classes)
    label_indices = find(Y_train==classes(i));
    grouped_data{i} = X_train(min(label_indices):max(label_indices),:);
end