How to find the constant value in a loop for a particular condition?

2 views (last 30 days)
A = [0.01; 0.03; 0.1; 0.3; 1; 3; 10; 30];
% error = zeros(64,1);
for i= 1:8
C = A(i);
for j = 1:8
sigma = A(j);
model= svmTrain(X, y, C, @(x1, x2) gaussianKernel(x1, x2, sigma));
predictions = svmPredict(model, Xval);
error(i,j) = mean(double(predictions ~= yval));
end
end
[p,q] = find(error == min(error(:)));
error gives me a 8x8 matrix. when I find minimum value of the matrix , it turns out that [5,3] indices of the matrix gives me the minimum value. Although I can find the corresponding C and sigma value manually as dimension of A is small. but if it is of high dimension then it is hard to find that. I just want to find out corresponding C and sigma value for which I get minimum value in the error matrix automatically.

Accepted Answer

KALYAN ACHARJYA
KALYAN ACHARJYA on 20 Feb 2021
Edited: KALYAN ACHARJYA on 20 Feb 2021
"I just want to find out corresponding C and sigma value for which I get minimum value in the error matrix automatically"
for i= 1:8
for j = 1:8
model= svmTrain(X, y, C, @(x1, x2) gaussianKernel(x1, x2, sigma));
predictions = svmPredict(model, Xval);
error(i,j) = mean(double(predictions ~= yval));
end
end
As you have complete the iterations to find the error matrix, once you done that, find the indices (i and j) of minimum of error matrix.
[i,j] = find(error==min(error(:)));
Now get the corresponding C and sigma
C=A(i)
sigma=A(j)
Note that, if you have mutiple minimum same value in the matrix, you may get multiple indices of i and j

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!