Index in position 2 exceeds array bounds (must not exceed 1).

8 次查看(过去 30 天)
load featurs_T
load featurs_S
load Group_Train
load Group_Test
load actual_label
load predict_label
load predict_label
%WEIGHTEDVOTING is a proposed ensemble method
k = max(actual_label); % determining number of classes [1-K]
heus = [];
%HOG_SVM = multisvm(Feat1,Group_Train1,Feat2,Group_Test1);
rec = recall(predict_label, actual_label); prec = precision(predict_label, actual_label);
heus = [heus; 2*f1(prec,rec)-FPR(predict_label, actual_label)];
%LBP_SVM = multisvm(Feat1,Group_Train1,Feat2,Group_Test1);
rec = recall(predict_label2, actual_label); prec = precision(predict_label2, actual_label);
heus = [heus; 2*f1(prec,rec)-FPR(predict_label2, actual_label)];
res = zeros(length(actual_label),length(k));
for i=1:length(actual_label)
res(i,predict_label(i)) = res(i,predict_label(i)) + heus(1);
res(i,predict_label2(i)) = res(i,predict_label2(i)) + heus(2);
end
[~, preds] = max(res,[],2);
Index in position 2 exceeds array bounds (must not exceed 1).
Error in weightedVoting (line 27)
res(i,predict_label(i)) = res(i,predict_label(i)) + heus(1);

采纳的回答

KSSV
KSSV 2022-5-29
This is a simple error. You are trying to extract more number of elements then present in the array.
% Example
A = rand(1,5) ; % size of A is 1x5
A(1) % no error
ans = 0.2750
A(4) % no error
ans = 0.3824
A(end) % no error, this will give you 5th element
ans = 0.8093
A(6) % error, there is no 6th element
Index exceeds the number of array elements. Index must not exceed 5.
Like wise check the dimensions of each array and use looping.

更多回答(2 个)

Walter Roberson
Walter Roberson 2022-5-29
A predicted label might exceed the number of actual labels.
Or heus might be empty, if some of the other variables are empty.
  3 个评论
Walter Roberson
Walter Roberson 2022-5-29
k is the maximum label, which is a scalar. length(k) is 1. You use length(k) as the upper bound for the size of res so res is 120 by 1 instead of 120 by the number of labels
sun rise
sun rise 2022-5-31
The error is due to the difference in the type of the array. How can I change the type of the array so that the code works.Look at the workspace
Error using categorical/max (line 63)
Relational comparisons are not allowed for categorical arrays that are not ordinal.
Error in recall (line 4)
K = max(actual_label); %number of classes
Error in weightedVoting (line 14)
rec = recall(predict_label, Group_Test1); prec = precision(predict_label, Group_Test1);

请先登录,再进行评论。


sun rise
sun rise 2022-5-31
%label.m
setup_folders;
sadtrdinfo = dir(sad1train); % Returns both folders and files
% sad = dir(pwd); % Returns both folders and files
sorted_cell_array_of_train_folder_names = setdiff({sadtrdinfo([sadtrdinfo.isdir]).name},{'..','.'}); % Select folder names
% cell_array_of_train_folder_names( strncmp( cell_array_of_train_folder_names, ".", 1 ) ) = []; % Remove '.' and '..'
%sorted_cell_array_of_train_folder_names = sort_nat( cell_array_of_train_folder_names );
% sorted_cell_array_of_train_folder_names = cell_array_of_train_folder_names; % if you don't have sort_nat
whos sorted_cell_array_of_train_folder_names
%----------------
np = numel(sorted_cell_array_of_train_folder_names); % number of subfolders
train_cats = categorical(sorted_cell_array_of_train_folder_names);
Group_Train1 = cell(np,1);
for i = 1 : np
SFN = sorted_cell_array_of_train_folder_names{i};% extract his name
traintifpattern = fullfile(sad1train, SFN, tiffwild);
tifListdinfo = dir(traintifpattern); % list all jpg files
tifList = fullfile({tifListdinfo.folder}, {tifListdinfo.name});
ms1 = numel(tifList); % ms = number of image files found
Group_Train1{i} = repmat(train_cats(i), ms1, 1);
end
Group_Train1 = vertcat(Group_Train1{:});
save('Group_Train','Group_Train1');
%---------------------
sadtedinfo = dir(sad1test); % Returns both folders and files
sorted_cell_array_of_test_folder_names = setdiff({sadtedinfo([sadtedinfo.isdir]).name},{'..','.'}); % Select folder names
whos sorted_cell_array_of_test_folder_names
%----------------
np = numel(sorted_cell_array_of_test_folder_names); %number of subfolders
test_cats = categorical(sorted_cell_array_of_test_folder_names);
Group_Test1 = cell(np, 1);
for i = 1 : np
SFN = sorted_cell_array_of_test_folder_names{i};% extract his name
testtifpattern = fullfile(sad1test, SFN, tiffwild);
tifListdinfo = dir(testtifpattern); % list all jpg files
tifList = fullfile({tifListdinfo.folder}, {tifListdinfo.name});
ms1 = numel(tifList); % ms = number of image files found
Group_Test1{i} = repmat(test_cats(i), ms1, 1);
end
Group_Test1 = vertcat(Group_Test1{:});
save('Group_Test','Group_Test1');
%HOG.M
setup_folders;
Folder = sad1train;
FileList = dir(fullfile(Folder, '**', tiffwild));
Feature = cell(1, numel(FileList)); % Pre-allocation
for iFile = 1:numel(FileList)
File = fullfile(FileList(iFile).folder, FileList(iFile).name);
Img = imread(File);
Img = imresize(Img, [128, 128]);
Feature{iFile} = extractHOGFeatures(Img,'CellSize',[2 2],'BlockSize',[2 2]);
end
% Maybe:
Feat1 = cat(1, Feature{:}); % Or cat(2, ...) ?!
clear Feature
save('featurs_T.mat', '-v7.3', 'Feat1');
%clear Feat1
Folder2 = sad1test;
FileList2 = dir(fullfile(Folder2, '**', tiffwild));
Feature2 = cell(1, numel(FileList2)); % Pre-allocation
for iFile2 = 1:numel(FileList2)
File2 = fullfile(FileList2(iFile2).folder, FileList2(iFile2).name);
Img2 = imread(File2);
%Img2 = imresize(Img2, [128, 128]);
Img2 = imresize(Img2, [128, 128]);
%Feature2{iFile2} = hog_feature_vector(Img2);
Feature2{iFile2} = extractHOGFeatures(Img2,'CellSize',[2 2],'BlockSize',[2 2]);
end
% Maybe:
Feat2 = cat(1, Feature2{:}); % Or cat(2, ...) ?!
clear Feature2
save('featurs_S.mat', '-v7.3', 'Feat2');
%clear Feat2
%maltisvm
function [preds] = multisvm(TrainingSet,Group_Train1,TestSet,Group_Test1)
%Models a given training set with a corresponding group vector and
%classifies a given test set using an SVM classifier according to a
%one vs. all relation.
%
%This code was written by Cody Neuburger cneuburg@fau.edu
%Florida Atlantic University, Florida USA...
%This code was adapted and cleaned from Anand Mishra's multisvm function
%found at http://www.mathworks.com/matlabcentral/fileexchange/33170-multi-class-support-vector-machine/
u=unique(Group_Train1);
numClasses=length(u);
%preds = zeros(length(TestSet(:,1)),1);
%build models
preds = categorical.empty();
for k=1:numClasses
%Vectorized statement that binarizes Group
%where 1 is the current class and 0 is all other classes
G1vAll=(Group_Train1==u(k));
models{k} = fitcsvm(TrainingSet,G1vAll,'KernelFunction','polynomial','polynomialorder',3,'Solver','ISDA','Verbose',0,'Standardize',true);
if ~models{k}.ConvergenceInfo.Converged
fprintf('Training did not converge for class "%s"\n', string(u(k)));
end
end
%classify test cases
for t=1:size(TestSet,1)
matched = false;
for k = numClasses:-1:1
% for k =1: numClasses
if(predict(models{k},TestSet(t,: )))
matched = true;
break;
end
end
if matched
preds(t,1) = u(k);
%result(t) = u(k);
else
preds(t,1) = 'No Match';
%--------------------------------
end
end
Accuracy = mean(Group_Test1==preds) * 100;
fprintf('Accuracy = %.2f\n', Accuracy);
fprintf('error rate = %.2f\n ', mean(preds ~= Group_Test1 ) * 100);
%confusionchart(result,Group_Test1);
end
%maltisvmcall
load featurs_T
load featurs_S
load Group_Train
load Group_Test
fprintf("HOG_SVM:\n")
HOG_SVM= multisvm(Feat1,Group_Train1,Feat2,Group_Test1);
predict_label= HOG_SVM;
%predict_label= double( predict_label);
save('predict_label.mat', 'predict_label');

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by