Input-Looping Matrix Dimensions Must Agree
1 view (last 30 days)
Show older comments
I got this error in very early morning:
Error using - Matrix dimensions must agree.
Error in DistMatrix3 (line 23)
d=sqrt((I-L').^2+(J-M').^2+(K-N').^2);
Error in coba (line 20)
d=DistMatrix3(cobat,c); % calculate the distance
And here is the code of mine: load cobat.txt
k=input('Enter a number: ');
[maxRow, maxCol]=size(cobat);
for i=1:k
for j=1:maxCol
c=input('Enter a number: '); % sequential initialization
end
end
temp=zeros(maxRow,1); % initialize as zero vector
u=0;
while 1,
d=DistMatrix3(cobat,c); % calculate the distance
[z,g]=min(d,[],2); % set the matrix g group
if g==temp, % if the iteration doesn't change anymore
break; % stop the iteration
else
temp=g; % copy the matrix to the temporary variable
end
**for i=1:k
f=find(g==i);
if f % calculate the new centroid
c(i,:)=mean(cobat(find(g==i),:),1);**
end
end
end
y=[cobat,g]
I think the error isn't because the DistMatrix3 itself. It is caused by 'c' inputs. the looping-input. And I guess these lines that should be fixed since it doesn't contain 'j' variables:
for i=1:k
f=find(g==i);
if f % calculate the new centroid
c(i,:)=mean(cobat(find(g==i),:),1);
end
I'm stuck how I should fix it. Can you guys tell me what should I do to solve this this? Your help will be much appreciated.
Thank you.
1 Comment
AS
on 15 Sep 2020
I am using this code but not understood about number of iteration i.e how many iteration are there in the code
Answers (0)
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!