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

2 次查看(过去 30 天)
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.

采纳的回答

KALYAN ACHARJYA
KALYAN ACHARJYA 2021-2-20
编辑:KALYAN ACHARJYA 2021-2-20
"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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by