how to find minimum value of output corresponds to which input values in a loop/nested loop ? Error i am getting is Index exceeds the number of array elements.

1 view (last 30 days)
i want to find for what value of c and what value of hh , the genError is minimum. The error i am getting is Index exceeds the number of array elements. Index must not exceed 1.
load fisheriris
X = meas;
Y = species;
TTT=0;
for c=[0.05 0.1]
for hh=[0.1 1]
TTT=TTT+1;
t = templateSVM('Standardize',true,'KernelFunction','gaussian','kernelscale',c,'boxconstraint',hh);
Mdl = fitcecoc(X,Y,'Learners',t,'FitPosterior',true,...
'ClassNames',{'setosa','versicolor','virginica'},...
'Verbose',2);
CVMdl = crossval(Mdl);
genError(TTT) = kfoldLoss(CVMdl);
end
end
[mingenaratederror min_index] = min(genError);
c_kscale = c(min_index); % get the corresponding value from the c array
hh_boxcon = hh(min_index); % get the corresponding value from the hh array

Accepted Answer

Voss
Voss on 18 Mar 2022
Note that after a for loop completes, the value of the loop iterator is the value it had during the last iteration of the loop:
TTT=0;
for c=[0.05 0.1]
for hh=[0.1 1]
TTT=TTT+1;
end
end
c % last value of c is 0.1
c = 0.1000
hh % last value of hh is 1
hh = 1
(An exception to this is when the loop iterates over an empty array, in which case the iterator is empty after the loop, but that's not the case here.)
The point is that c and hh are scalars, so indexing them with min_index can generate the error you saw. If you want to find the values of c and hh that correspond to the minimum value of genError, it may be convenient to create c and hh as vectors with the appropriate sequences of values:
[hh,c] = ndgrid([0.1 1],[0.05 0.1]);
c = c(:)
c = 4×1
0.0500 0.0500 0.1000 0.1000
hh = hh(:)
hh = 4×1
0.1000 1.0000 0.1000 1.0000
Now c and hh have all the values they need, in the same order as they have in your nested loops, so you can replace your nested loops with a single loop, and the single loop can use TTT to index into c and hh:
load fisheriris
X = meas;
Y = species;
for TTT = 1:numel(c)
t = templateSVM( ...
'Standardize',true, ...
'KernelFunction','gaussian', ...
'kernelscale',c(TTT), ... % c is a vector now: need to index with TTT
'boxconstraint',hh(TTT)); % same for hh
Mdl = fitcecoc(X,Y, ...
'Learners',t, ...
'FitPosterior',true,...
'ClassNames',{'setosa','versicolor','virginica'},...
'Verbose',2);
CVMdl = crossval(Mdl);
genError(TTT) = kfoldLoss(CVMdl);
end
Training binary learner 1 (SVM) out of 3 with 50 negative and 50 positive observations. Negative class indices: 2 Positive class indices: 1 Fitting posterior probabilities for learner 1 (SVM). Training binary learner 2 (SVM) out of 3 with 50 negative and 50 positive observations. Negative class indices: 3 Positive class indices: 1 Fitting posterior probabilities for learner 2 (SVM). Training binary learner 3 (SVM) out of 3 with 50 negative and 50 positive observations. Negative class indices: 3 Positive class indices: 2 Fitting posterior probabilities for learner 3 (SVM). Training binary learner 1 (SVM) out of 3 with 50 negative and 50 positive observations. Negative class indices: 2 Positive class indices: 1 Fitting posterior probabilities for learner 1 (SVM). Training binary learner 2 (SVM) out of 3 with 50 negative and 50 positive observations. Negative class indices: 3 Positive class indices: 1 Fitting posterior probabilities for learner 2 (SVM). Training binary learner 3 (SVM) out of 3 with 50 negative and 50 positive observations. Negative class indices: 3 Positive class indices: 2 Fitting posterior probabilities for learner 3 (SVM). Training binary learner 1 (SVM) out of 3 with 50 negative and 50 positive observations. Negative class indices: 2 Positive class indices: 1 Fitting posterior probabilities for learner 1 (SVM). Training binary learner 2 (SVM) out of 3 with 50 negative and 50 positive observations. Negative class indices: 3 Positive class indices: 1 Fitting posterior probabilities for learner 2 (SVM). Training binary learner 3 (SVM) out of 3 with 50 negative and 50 positive observations. Negative class indices: 3 Positive class indices: 2 Fitting posterior probabilities for learner 3 (SVM). Training binary learner 1 (SVM) out of 3 with 50 negative and 50 positive observations. Negative class indices: 2 Positive class indices: 1 Fitting posterior probabilities for learner 1 (SVM). Training binary learner 2 (SVM) out of 3 with 50 negative and 50 positive observations. Negative class indices: 3 Positive class indices: 1 Fitting posterior probabilities for learner 2 (SVM). Training binary learner 3 (SVM) out of 3 with 50 negative and 50 positive observations. Negative class indices: 3 Positive class indices: 2 Fitting posterior probabilities for learner 3 (SVM).
Now finding the values of c and hh corresponding to the minimum value of genError will work correctly:
[mingenaratederror min_index] = min(genError);
c_kscale = c(min_index); % get the corresponding value from the c array
hh_boxcon = hh(min_index); % get the corresponding value from the hh array
disp(min_index);
4
disp(c_kscale);
0.1000
disp(hh_boxcon);
1

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices 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!