Index in position 2 exceeds array bounds. Index must not exceed 59. Error in FRM_PLS (line 27) features = trainFeaturesMat(:, i);
2 次查看(过去 30 天)
显示 更早的评论
This is the error for this code:
% PLS Method
% Load a dataset of grayscale face images
Dataset = imageDatastore('ExtendedYaleB', 'IncludeSubfolders', true, 'LabelSource', 'foldernames');
% Split the data into training and testing sets
[trainImgs, testImgs] = splitEachLabel(Dataset, 0.7, 'randomized');
% Extract local patches from the training images using the extractLBPFeatures function
numNeighbors = 8;
radius = 1;
trainFeatures = cell(numel(trainImgs.Files), 1);
for i = 1:numel(trainImgs.Files)
img = readimage(trainImgs, i);
trainFeatures{i} = extractLBPFeatures(img, 'NumNeighbors', numNeighbors, 'Radius', radius);
end
% Perform PLS regression
numComponents = 50; % Number of PLS components to retain
trainingLabels = double(trainImgs.Labels);
trainFeaturesMat = cell2mat(trainFeatures);
[~,~,~,~,betaPLS,~] = plsregress(trainFeaturesMat, trainingLabels, numComponents);
% Project train features onto PLS subspace
trainFeaturesPLS = cell(numel(trainImgs.Files), 1);
for i = 1:numel(trainImgs.Files)
features = trainFeaturesMat(:, i);
trainFeaturesPLS{i} = features' .* betaPLS;
end
% Train the linear regression model on the modified LBP features
trainFeaturesPLS = cell2mat(trainFeaturesPLS);
mdl = fitrlinear(trainFeaturesPLS, trainingLabels, 'Learner', 'leastsquares', 'Lambda', lambda);
% Save the model to a file
save('my_Model.mat', 'mdl', 'betaPLS');
% Extract local patches from the testing images and make predictions using the predict function
testFeatures = cell(numel(testImgs.Files), 1);
for i = 1:numel(testImgs.Files)
img = readimage(testImgs, i);
testFeatures{i} = extractLBPFeatures(img, 'NumNeighbors', numNeighbors, 'Radius', radius);
end
% Project test features onto PLS subspace
testFeaturesPLS = cell(numel(testImgs.Files), 1);
for i = 1:numel(testImgs.Files)
features = cell2mat(testFeatures(i));
testFeaturesPLS{i} = features' * betaPLS;
end
% Convert test features to matrix
testFeaturesPLS = cell2mat(testFeaturesPLS);
% Make predictions using the loaded model
load('my_Model.mat', 'mdl');
predictions = predict(mdl, testFeaturesPLS);
% Evaluate the performance of the model using the confusionmat and classificationReport functions
confMat = confusionmat(testImgs.Labels, predictions);
classificationReport = classificationReport(testImgs.Labels, predictions);
% Use the loaded model for prediction
testImg = imread('test_image.jpg');
testFeatures = extractLBPFeatures(testImg, 'NumNeighbors', numNeighbors, 'Radius', radius);
testFeaturesPLS = testFeatures' * betaPLS;
prediction = predict(mdl, testFeaturesPLS);
Suggest a solution
0 个评论
回答(1 个)
Cris LaPierre
2023-8-10
编辑:Cris LaPierre
2023-8-12
numel(testImgs.Files) is apparently greater than the number of columns in trainFeaturesMat. The number of columns is 59, so when your loop index exceeds the number of columns, your indexing operation throws the error you are seeing.
Here is a simplified example of the error.
A = 1;
% This works
A(1,1)
% This produces the same error
A(1,2)
You need to write your code in such a way so that your index does not exceed your variable dimensions. Perhaps something like this?
for i = 1:size(trainFeaturesMat,2)
features = trainFeaturesMat(:, i);
trainFeaturesPLS{i} = features' .* betaPLS;
end
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!