MATLAB: Index exceeds matrix dimensions.

errorindexmatrixscattermat

Error in the command window
li=X1(clsi,:);
This how the program looks
function [W ,B]=scattermat(X1,y1)
%FUNCTION THAT CALCULATES SCATTER MATRIX:
% B:BETWEEN CLASS SCATTER MATRIX
% W:WITHIN CLASS SCATTER MATRIX
%
[~, l]=size(X1); %CALCULATE SIZE OF DATA
clases=unique(y1); %GET VECTOR OF CLASSES
total_clases=length(clases); %HOW MANY CLASSES
B=zeros(l,l); %INIT B AND W
W=zeros(l,l);
ovrmean=mean(X1); %MEAN OVER ALL DATA
for i=1:total_clases
clsi = find(y1==clases(i)); %GET DATA FOR EACH CLASS
display(clsi);
li=X1(clsi,:);
mclsi=mean(li); %MEAN PER CLASS
li=li-repmat(mclsi,length(clsi),1); %Xi-MeanXi
B=B+li'*li; %CALCULATE B
W=W+length(clsi)*(mclsi-ovrmean)'*(mclsi-ovrmean); %CALCULATE W
end
end
How do I resolve this issue ??? I am new to matlab. Please help me out.

Best Answer

That could happen if the input vector y1 is not the same length as the number of rows in the input X1.