Selecting rows based on number in first row and assigining it to a new variable name

1 view (last 30 days)
Hello,
I'm triying to fit my training data to my test data and split everything up. there's six different actvities I'm trying to filter, so i tried this:
for n = 1:6
eval(['X_test_' num2str(n) '= X_test_y(X_test_y(:,1) == n),:)']);
end
So the desired outcome is to get X_test_1, which contains the data of X_test_y where the first column contains a 1.
and so on for X_test_2 , etc
this doesn't work, but when I try X_test_1 = X_test_y(X_test_y(:,1) == 1,:); it does work to get a good X_test_1.
any tips on how to do this in a For loop?

Answers (1)

Guillaume
Guillaume on 2 Feb 2020
The answer to your question is simple: don't do that
This post explains the many reasons why it's a very bad idea to dynamically name variables. Use a cell array, a structure, a map, whatever data type of your liking that can be easily indexed instead.

Categories

Find more on Statistics and Machine Learning Toolbox in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!