Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

New to MATLAB?

Error using ==> plus Matrix dimensions must agree.

Asked by rakesh balusa

rakesh balusa (view profile)

on 19 Sep 2013

load irisdataset.txt DataSet=irisdataset;


%----------------------- Selecting 3 random centres ----------------------% Selection=rand(1,3); Selection=Selection*Dim(1,1); Selection=ceil(Selection); %Selecting the row number.

%--------------------------- 3 random centres ----------------------------% Centre1=DataSet(Selection(1),:); Centre2=DataSet(Selection(2),:); Centre3=DataSet(Selection(3),:);

n=input('No Of Iterations : ') %----------------------- Partitional Algorithm ---------------------------% %-------------------------- K means Algorithm ----------------------------% for j=1:1:n count1=0; Mean1=zeros(1,4); count2=0; group1=[]; Mean2=zeros(1,4); group2=[]; count3=0; group3=[]; Mean3=zeros(1,4);

%Finding the minimum distance for i=1:1:Dim(1,1)

Pattern1(i)=sqrt((Centre1(1,1)-DataSet(i,1))^2+(Centre1(1,2)-DataSet(i,2))^2+(Centre1(1,3)-DataSet(i,3))^2+(Centre1(1,4)-DataSet(i,4))^2); Pattern2(i)=sqrt((Centre2(1,1)-DataSet(i,1))^2+(Centre2(1,2)-DataSet(i,2))^2+(Centre2(1,3)-DataSet(i,3))^2+(Centre1(1,4)-DataSet(i,4))^2); Pattern3(i)=sqrt((Centre3(1,1)-DataSet(i,1))^2+(Centre3(1,2)-DataSet(i,2))^2+(Centre3(1,3)-DataSet(i,3))^2+(Centre1(1,4)-DataSet(i,4))^2);

LessDist=[Pattern1(i) Pattern2(i) Pattern3(i)]; Minimum=min(LessDist);

%Finding the new centre if (Minimum==Pattern1(i)) count1=count1+1; Mean1=Mean1+DataSet(i,:); group1=[group1 i]; else if (Minimum==Pattern2(i)) count2=count2+1; Mean2=Mean2+DataSet(i,:); group2=[group2 i]; else count3=count3+1; Mean3=Mean3+DataSet(i,:); group3=[group3 i]; end end


%----------------------------- New Centres -------------------------------% Centre1=Mean1/count1; Centre2=Mean2/count2; Centre3=Mean3/count3; plot(j,count1,'r'); hold on plot(j,count2,'g'); plot(j,count3,'b');


hold off % specify the indexed color for each point icolor = ceil((DataSet(:,4)/max(DataSet(:,4)))*256);

figure, scatter3(DataSet(:,1),DataSet(:,2),DataSet(:,3),DataSet(:,4),icolor,'filled'); figure,


1 Comment

Jan Simon

Jan Simon (view profile)

on 19 Sep 2013

Please format your code properly when you want encourage others to read it. Follow the "? Help" link to learn more about formatting in this forum.

Posting the complete error message is a good method to allow the readers to recognize which line causes the problem. We cannot guess such details, but we cannot run you code also due to the missing input file.

rakesh balusa

rakesh balusa (view profile)



No products are associated with this question.

1 Answer

Answer by Jan Simon

Jan Simon (view profile)

on 19 Sep 2013

The debugger is the right tool to solve such problems:

dbstop if error

The start your function again. When it stops check the dimensions of the local variables e.g. by typing "size(...)" in the command window.


Jan Simon

Jan Simon (view profile)

Contact us