Why do I get the " Error using ==> mldivide Matrix dimensions must agree." error message?

1 view (last 30 days)
I just check my ex from my book, and that error message show up
here the script
clear;
clc;
% mendefinisikan parameter dari permasalahan yang akan diselesaikan
jumlah_kota = 15;
kota = floor (20*rand(jumlah_kota,2));
for i = 1 : jumlah_kota
for j = 1 : jumlah_kota
jarak_antar_kota(i,j) = sqrt((kota(i,1)- kota(j,1))^2 + (kota(i,2) - kota(j,2))^2);
end
end
% Menentukan kriteria berhenti
iterasi_maksimum = 50;
% mengatur parameter ant colony
jumlah_semut = 20;
pheromone = 0.5*ones(jumlah_kota,jumlah_kota);
tetha = 1./jarak_antar_kota;
alpha = 1;
betha = 2;
rho = 0.5;
Q = 2.5;
% Prosedur Ant Colony Optimization
% Inisialisasi Koloni Semut
for i = 1 : jumlah_semut
rute(i,:) = randperm(jumlah_kota);
end
rute(:,jumlah_kota+1) = rute(:,1);
for i = 1 : jumlah_semut
jarak(i,j) = 0;
for j = 1 : jumlah_kota
jarak(i,j) = jarak(i) + jarak_antar_kota(rute(i,j),rute(i,j+1));
end
end
[jarak_terbaik,idx_semut] = min(jarak);
rute_awal = rute(idx_semut,:);
% update pheromone
delta_pheromone = zeros(jumlah_kota,jumlah_kota);
for i = 1 : jumlah_semut
for j = 1 : jumlah_kota
delta_pheromone(rute(i,j),rute(i,j+1)) = Q/jarak_terbaik;
end
end
pheromone = (1-rho)*pheromone + delta_pheromone;
iterasi = 0
while iterasi <= iterasi_maksimum
iterasi = iterasi+1;
for i = 1 : jumlah_semut
rute_temporary = rute(i,:);
for j = 2 : jumlah_kota - 1
kota_sekarang = rute(i,j-1);
daftar_kota_berikutnya = rute(i,j:jumlah_kota);
probabilitas = ((pheromone(kota_sekarang,daftar_kota_berikutnya).^alpha).*(tetha(kota_sekarang,daftar_kota_berikutnya).^betha))./sum((pheromone(kota_sekarang,daftar_kota_berikutnya).^alpha).*(tetha(kota_sekarang,daftar_kota_berikutnya).^betha));
r = rand();
kota_selanjutnya = j;
for k = 1 : length(probabilitas)
if r < sum(probabilitas(1,k))
kota_selanjutnya = j+k-1
break
end
end
kota_berikutnya = rute(i,kota_selanjutnya);
rute(i,kota_selanjutnya) = rute(i,j) ;
rute(i,j) = kota_berikutnya;
end
end
% menghitung jarak
pheromone_sementara = zeros(jumlah_kota,jumlah_kota);
for i = 1 : jumlah_semut
jarak(i,j) = 0;
for j = 1 : jumlah_kota
jarak(i,1) = jarak(i) + jarak_antar_kota(rute(i,j),rute(i,j+1));
end
end
% update solusi terbaik
[jarak_min,idx_semut] = min(jarak);
if jarak_min < jarak_terbaik
jarak_terbaik = jarak_min;
end
%update pheromone
delta_pheromone = zeros(jumlah_kota,jumlah_kota);
for j = 1 : jumlah_kota
delta_pheromone (rute(idx_semut,j),rute(idx_semut,j+1)) = Q/jarak_terbaik;
end
%update pheromone
pheromone = (1-rho)*pheromone + delta_pheromone;
min_jarak (iterasi) = min(jarak);
mean_jarak (iterasi) = mean(jarak);
end
[jarak_min,idx_semut] = min(jarak);
figure(1)
it=0;length(min_jarak)-1;
plot(it,min_jarak);
xlabel('iterasi');ylabel('nilai objektif');
title('Jarak tempuh terbaik','FontWeight','bold');
figure(2);
rute0=[ruteAwal ruteAwal (1)];
subplot(1,2,1);
plot(kota(rute0,1),kota(rute0,2),'o-');
title('rute awal','FontWeight','bold');
semut_terbaik = rute(idx_semut,:);
rute=[semut_terbaik semut_tebaik(1)];
subplot(1,2,2);
plot(kota(rute,1),kota(rute,2),'o-');
title('rute terbaik','FontWeight','bold');
this is the error message :
??? Error using ==> mldivide
Matrix dimensions must agree.
Error in ==> ANT at 53
delta_pheromone(rute(i,j),rute(i,j+1)) = Q/jarak_terbaik;
  1 Comment
Stephen23
Stephen23 on 4 Jan 2015
Something is a bit strange with this error: the message apparently refers to a line that does not contain any instance of mldivide, but it is difficult to tell as the code is not formatted. Please edit your question and format the code correctly using the button {} Code that you can find above the text box.

Sign in to comment.

Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!