error in plotting Index in position 2 exceeds array bounds.

조회 수: 1 (최근 30일)
Tomasz Kaczmarski
Tomasz Kaczmarski 2020년 2월 10일
댓글: Tomasz Kaczmarski 2020년 2월 18일
Hello
Im quite new to matlab
currently im trying to plot as in
using dataset from
my code is below
clear; close all; clc;
T = readtable('train.csv');
X = T(:,(1:562));
Y = T(:,{'Activity'});
t = templateSVM('Standardize',true,'SaveSupportVectors',true);
predictorNames = {'alfa','beta'};
responseName = 'Human Activity';
classNames = {'STANDING','SITTING','LAYING','WALKING','WALKING_DOWNSTAIRS','WALKING_UPSTAIRS'}; % Specify class order
Mdl = fitcecoc(X,Y);
Mdl.ClassNames
Mdl.CodingMatrix
L = size(Mdl.CodingMatrix,6); % Number of SVMs
sv = cell(L,1); % Preallocate for support vector indices
for j = 1:L
SVM = Mdl.BinaryLearners{j};
sv{j} = SVM.SupportVectors;
sv{j} = sv{j}.*SVM.Sigma + SVM.Mu;
end
hold on
markers = {'ko','ko','ko','ko','ko','ko','ko'}; % Should be of length L
for j = 1:L
svs = sv{j};
plot(svs(:,1),svs(:,2),markers{j},...
'MarkerSize',10 + (j - 1)*3);
end
title('Fisher''s Iris -- ECOC Support Vectors')
xlabel(predictorNames{1})
ylabel(predictorNames{2})
legend([classNames,{'Support vectors - SVM 1',...
'Support vectors - SVM 2','Support vectors - SVM 3'}],...
'Location','Best')
hold off
unfortunettly im getting error
Index in position 2 exceeds array bounds.Index in position 2 exceeds array bounds.
Error in learningmulticlasCVN (line 35)
plot(svs(:,1),svs(:,2),markers{j},..
screen.PNG

채택된 답변

Rajani Mishra
Rajani Mishra 2020년 2월 13일
편집: Rajani Mishra 2020년 2월 13일
I tried running your code and debugged for the error encountered. Error – “Exceeds array bounds” is faced when index out of array size is accessed, which was the case in the line with a call to plot() function.
I have made some changes in the code provided:
  1. I have changed as the way input X and Y are passed to function to fitcecoc():
T = readtable('train.csv');
Y = T(:,{'Activity'});
X = T(:,(1:562));
X = X{:,:};
Y = Y{:,:};
t = templateSVM('Standardize',true,'SaveSupportVectors',true);
predictorNames = {'alfa','beta'};
responseName = 'Human Activity';
classNames = {'STANDING','SITTING','LAYING','WALKING','WALKING_DOWNSTAIRS','WALKING_UPSTAIRS'}; % Specify class order
Mdl = fitcecoc(X,Y,'Learners',t,'ResponseName',responseName,'ClassNames',classNames)
2. I have changed the second argument passed to size () function, as the second argument defines the dimension along which you want to calculate the size of the matrix. As here “Mdl.Codingmatrix” is of size 6* 16, its second dimension whose size is required.
L = size(Mdl.CodingMatrix,2); % Number of SVMs
3. Definition of markers array
markers = {'ko','ko','ko','ko','ko','ko','ko','ko','ko','ko','ko','ko','ko','ko','ko'}; % Should be of length L
I have attached the result after making the above changes.
Hope this helps!

추가 답변 (0개)

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by