Append results of two 'for loops' into a single variable
2 次查看(过去 30 天)
显示 更早的评论
I'm running an SVM ML template from MATLAB and I need to note down the accuracies for each parameter in 10 runs (with different training and test sets (which i already have prepared). So, I have put this in a 'for loop' which runs for 10 times and saves the accuracies each time (10 accuracies) into a variable named 'a1'. Now, I have to repeat this procedure for another parameter using another 'for loop' and save the results in the same variable 'a1' without rewriting the results obtained from previous loop. Ultimately I want all the accuracies obtained from all the loops/different loops in a single variable. Kindly help me with this.
Thank you in advance.
itr_3 = 1:10;
%%% 1 feature
FeaturesSelected = horzcat(feature1,fs_Y);
colNames = {'Feature1','Label'};
FeaturesSelected_Table = array2table(FeaturesSelected,'VariableNames',colNames);
for i=1:length(itr_3)
X_Train = X(FeaturesSelected_Table(1:80,:));
Y_Train = Y(FeaturesSelected_Table(1:80,:));
X_Test = X(FeaturesSelected_Table(81:100,:));
Y_Test = Y(FeaturesSelected_Table(81:100,:));
t = templateSVM('Standardize',true,'KernelFunction','rbf','SaveSupportVectors',true);
Mdl = fitcecoc(X_Train,Y_Train,'Learners',t,'FitPosterior',true,'Coding','onevsall', 'Verbose',2)
[predictClass, scoresSVM]=predict(Mdl,X_Test);
disp('class predict')
disp([Y_Test predictClass])
result = [Y_Test predictClass];
Accuracy=mean(Y_Test==predictClass)*100;
fprintf('\nAccuracy =%d\n',Accuracy)
a{i}(:,1)=Accuracy;
end
%%% 2 feature
FeaturesSelected = horzcat(feature1,feature2,fs_Y);
colNames = {'Feature1','Feature2','Label'};
FeaturesSelected_Table = array2table(FeaturesSelected,'VariableNames',colNames);
for i=1:length(itr_3)
X_Train = X(FeaturesSelected_Table(1:80,:));
Y_Train = Y(FeaturesSelected_Table(1:80,:));
X_Test = X(FeaturesSelected_Table(81:100,:));
Y_Test = Y(FeaturesSelected_Table(81:100,:));
t = templateSVM('Standardize',true,'KernelFunction','rbf','SaveSupportVectors',true);
Mdl = fitcecoc(X_Train,Y_Train,'Learners',t,'FitPosterior',true,'Coding','onevsall', 'Verbose',2)
[predictClass, scoresSVM]=predict(Mdl,X_Test);
disp('class predict')
disp([Y_Test predictClass])
result = [Y_Test predictClass];
Accuracy=mean(Y_Test==predictClass)*100;
fprintf('\nAccuracy =%d\n',Accuracy)
a{i}(:,1)=Accuracy;
end
0 个评论
回答(1 个)
Davide Masiello
2022-4-26
编辑:Davide Masiello
2022-4-26
You could store the accuracies of the first loop into a variable named a1 and those of the second loop into a variables named a2.
Then, after the loops, you can concatenate the two arrays.
% first loop
for i = 1:length(itr_3)
...
...
a1(i) = Accuracy;
end
% second loop
for i = 1:length(itr_3)
...
...
a2(i) = Accuracy;
end
a = [a1,a2];
Please note that I have changed the indexing so that the accuracies are stored as arrays of doubles and not cell arrays.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!