Index exceeds the number of array elements (20) Error

cvx=cvpartition(size(Features,1),'kfold',5);
numvalidsets = cvx.NumTestSets;
n = cvx.TrainSize(1);
lambdavals = linspace(0,20,20)/n;
lossvals = zeros(length(lambdavals),numvalidsets);
for w = 1:length(lambdavals)
for p =1:numvalidsets
train=1;
test=1;
indextrain=training(cvx,p);
for i=1:size(Features,1)
if indextrain(i)==1
XTrain(train,:)=Features(i,:);
YTrain(train)=label(i);
train=train+1;
else
XTest(test,:)=Features(i,:);
YTest(test)=label(i);
test=test+1;
end
end
TrainData= XTrain,YTrain;
TestData =XTest,YTest;
nca = fscnca(XTrain,YTrain,'FitMethod','exact', ...
'Solver','sgd','Lambda',lambdavals(i), ...
'IterationLimit',30,'GradientTolerance',1e-4, ...
'Standardize',true);
lossvals(w,p) = loss(nca,XTest,YTest,'LossFunction','classiferror');
end
end
i recived this error ''Index exceeds the number of array elements (20).'' the error is from the fourth line ''lambdavals = linspace(0,20,20)/n;''
as the feature size is 700x16384 single and label size is 1x700 double. any help how to set the linspace () parameters i will be approchated.

 Accepted Answer

lambdavals=(linspace(0,20,20))./n;

8 Comments

unfortunately, i still got the same error
n is the number of training videos which is here 560
I have verified again
>> n=560;
>> lambdavals=(linspace(0,20,20))./n
lambdavals =
Columns 1 through 12
0 0.0019 0.0038 0.0056 0.0075 0.0094 0.0113 0.0132 0.0150 0.0169 0.0188 0.0207
Columns 13 through 20
0.0226 0.0244 0.0263 0.0282 0.0301 0.0320 0.0338 0.0357
Are you sure the error in the same line?
could you please explain what these numbers in parameters mean
linspace cretes the linearly spaced vector with initial values in the range 0-20
The length of the vector is 20
linspace(0,20,20)
%........0 to 20 range 20 values
See example
>> linspace(0,1,5)
ans =
0 0.2500 0.5000 0.7500 1.0000
After it creation, all elements are divide by n
Thank you for your explanation, and your time, the code is working now.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!