Selecting rows based on number in first row and assigining it to a new variable name
3 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
回答(1 个)
Guillaume
2020-2-2
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Statistics and Machine Learning Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!