How to find the constant value in a loop for a particular condition?
조회 수: 1 (최근 30일)
이전 댓글 표시
Ritesh Chandra Tewari
2021년 2월 20일
댓글: Ritesh Chandra Tewari
2021년 2월 20일
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.
댓글 수: 0
채택된 답변
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 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!