Clear Filters
Clear Filters

How to solve "Subscripted assignment dimension mismatch" error?

2 views (last 30 days)
Hi everyone, please help me.
close all;
clear all;
clc;
% Dien tich mang xm*ym(m2) %
xm = 100;
ym = 100;
% Thiet lap thong so mang %
nodes = 50; %So nut mang
p = 0.1; %Ti le nut chu
cluster_head = p*nodes; %So nut chu
R = 30; %Ban kinh phat toi da ban tin quang ba cua nut chu(m)
Pt = 30; %Cong suat phat ban tin quang ba cua nut chu(mW)
Pl = 1; %Cong suat suy hao do khoang cach(mW/m)
to = 0.001; %Thoi gian lan truyen ban tin tren mot don vi quang duong truyen(s/m)
% Nang luong $
%Nang luong cho ban dau cua mot nut
Eo = 2; %Don vi J
%Nang luong tieu ton de xu ly mot bit trong mot nut
Eelec = 50*10^(-9); %Don vi J/b
%Nang luong bo khuech dai dung de lam viec
Eamp = 100*10^(-12); %Don vi J/b/m^2
%Nang luong dung de tong hop du lieu
EDA = 5*10^(-9); %Don vi J/b
% Kich thuoc goi tin %
k = 4000; %Don vi bit
% Vi tri nut Sink %
xsink = 50;
ysink = 150;
% So chu ky toi da %
r = 10;
% Khoi tao bien so CL %
CLs = 1; %Bien so de tinh toan
% Khoi tao bien dem so nut duoc ket noi voi nut chu cum
con_nodes = 0;
% Khoi tao bien dem so nut khong ket noi dc voi nut chu cum
dis_nodes = 0;
figure('Name','Mang cam bien khong day su dung giao thuc LEACH');
% Thiet lap mang %
h1 = plot(xsink,ysink,'s','LineWidth',2,...
'MarkerEdgeColor','k',...
'MarkerFaceColor','g',...
'MarkerSize',15);
hold on;
for i = 1:nodes
SN(i).id = i; %Dia chi nut mang
SN(i).x = rand*xm; %Hoanh do nut mang
SN(i).y = rand*ym; %Tung do nut mang
SN(i).CL = 0; %Bang 0 thi la nut binh thuong, bang 1 thi la nut chu cum
SN(i).rleft = 0; %Chu ky khong duoc lam nut chu(Thiet lap ban dau)
SN(i).E = Eo; %Nang luong ban dau cua nut
SN(i).S = 1; %Tinh trang ban dau cua nut 1=LIVE 0=DIE
hold on;
h2 = plot(SN(i).x,SN(i).y,'o','LineWidth',2,...
'MarkerEdgeColor','r',...
'MarkerFaceColor','y',...
'MarkerSize',10);
end
% Khoi tao bien tong nang luong tieu ton %
e = 0;
for t = 1:r
% Thiet lap cluster head %
for j = 1:nodes
SN(j).g = rand;
if ( SN(j).g < p/(1-p*mod(r,1/p)) ) && ( i ~= SN(j).rleft ) && ( SN(j).S == 1)
CL(j).id = SN(j).id;
CL(j).x = SN(j).x;
CL(j).y = SN(j).y;
% Khoang cach giua nut chu va nut Sink %
CL(j).dS = sqrt((CL(j).x-xsink)^2+(CL(j).y-ysink)^2);
CL(CLs) = CL(j);
SN(j).rleft = 1/p-mod(r,1/p);
SN(j).CL = 1;
CLs = CLs+1;
h3 = plot(CL(j).x,CL(j).y,'o','LineWidth',2,...
'MarkerEdgeColor','m',...
'MarkerFaceColor','k',...
'MarkerSize',10);
draw_circle1(SN(j).x,SN(j).y,R,'g');
else SN(j).CL = 0;
end
end
ClusterHeads = CLs-1;
CL=CL(1:ClusterHeads);
disp('So nut chu cum la');
disp(ClusterHeads);
legend('Sink','Sensor Node');
xlabel('Truc hoanh(m)');
ylabel('Truc tung(m)');
title('Mo hinh mang cam bien khong day');
grid on;
% Gop cac node thanh cac cum %
for i = 1:ClusterHeads
for j = 1:nodes
if ( SN(j).CL ~= 1 ) && ( SN(j).S == 1 )
% Khoang cach giua nut binh thuong va cac nut chu cum %
dD(i,j) = sqrt((CL(i).x-SN(j).x)^2+(CL(i).y-SN(j).y)^2);
% Cong suat ban tin quang ba nhan duoc cua nut binh thuong %
dP(i,j) = Pt-Pl*dD(i,j)-10*rand; %Cong suat ton hao do nhieu Pf=10*rand
end
end
end
% Gop cum dua theo Cong suat ban tin quang ba nhan duoc %
for i=1:nodes
%Khoang cach giua nut thuong va nut chu cum SN(i).dD
%Cong suat ban tin quang ba lon nhat ma nut nhan duoc tu nut chu SN(i).dP
%Dia chi nut chu ma nut thuoc cum do SN(i).C
[SN(i).dP, col] = max(dP(:,i));
if (SN(i).CL ~= 1) && (SN(i).dP > 0.1) && ( SN(i).S == 1 )%Cong suat toi thieu de co the nhan duoc la 0.1mW
plot([SN(i).x CL(col).x], [SN(i).y CL(col).y], 'r');
SN(i).C = CL(col).id;
SN(i).dD = dD(col,i);
con_nodes = con_nodes+1;
CL(col).ETx1 = Pt*to*R; %Nang luong phat ban tin quang ba cua nut chu cum
SN(i).ERx = (Eelec+EDA)*k*1/4; %Nang luong nut binh thuong tieu hao thu nhan va xu ly du lieu tu ban tin quang ba
SN(i).ETx1 = Pt*to*dD(col,i); %Nang luong phat ban tin hoi dap cua nut thuong
e = e+SN(i).ERx+SN(i).ETx1;
else
if (SN(i).CL ~= 1)
SN(i).C = 0; %Nut chua thuoc cum nao
dis_nodes = dis_nodes+1;
SN(i).ETx1 = 0;
else
SN(i).C = -1; %Nut chu
SN(i).ETx1 = 0;
end
end
end
for i = 1:ClusterHeads
e = e+CL(i).ETx1;
plot([CL(i).x xsink], [CL(i).y ysink]);
end
disp('So nut duoc ket noi voi nut chu cum:');
disp(con_nodes);
disp('So nut khong duoc ket noi voi nut chu cum:');
disp(dis_nodes);
% Giai doan on dinh %
% Nang luong nut binh thuong tieu hao gui du lieu cho nut chu cum %
for i = 1:nodes
if ( SN(i).CL ~= 1 ) && ( SN(i).S == 1 ) && ( SN(i).C > 0)
SN(i).ETx2 = Eelec*k+Eamp*k*SN(i).dD;
e = e+SN(i).ETx2;
if ( SN(i).E-SN(i).ETx1-SN(i).ETx2-SN(i).ERx ) < 0.2 %Nut chet
SN(i).S = 0;
end
end
end
% Nang luong nut chu cum thu nhan va xu ly du lieu %
for i = 1:ClusterHeads
if ( SN( CL(i).id ).CL == 1 ) && ( SN( CL(i).id ).S == 1 )
CL(i).ERx = (Eelec+EDA)*k;
e = e + CL(i).ERx;
if ( SN( CL(i).id ).E-CL(i).ETx1-CL(i).ERx ) < 0.2 %Nut chet
SN( CL(i).id ).S = 0;
end
end
end
% Nang luong nut chu cum gui du lieu cho nut Sink %
for i = 1:ClusterHeads
if ( SN( CL(i).id ).CL == 1 ) && ( SN( CL(i).id ).S == 1 )
CL(i).ETx2 = (Eelec+EDA)*k + Eamp*k*CL(i).dS;
e = e + CL(i).ETx2;
if ( SN( CL(i).id ).E-CL(i).ETx1-CL(i).ERx-CL(i).ETx2 ) < 0.2 %Nut chet
SN( CL(i).id ).S = 0;
end
end
end
CL(:,:)=[];
end
aaa.PNG
  2 Comments
Bob Thompson
Bob Thompson on 10 May 2019
I was unable to reproduce your error, as CL(1) was only blank values. That is about the only consistent problem I run into, because you have the random number generator going for SN.g. Generally, the problem I am seeing is that the left side is looking for a single value, but the right side produces a blank cell. This could be the error that you're experiencing, but I don't get the same message. I would suggest either removing the blank values from CL, or finding some way of creating actual results for them.
Long Tran
Long Tran on 10 May 2019
Thanks for your suggestion.
I fixed it. And then, I got new error :D . Can you help me again?Capture.PNG
close all;
clear all;
clc;
% Dien tich mang xm*ym(m2) %
xm = 100;
ym = 100;
% Thiet lap thong so mang %
nodes = 50; %So nut mang
p = 0.1; %Ti le nut chu
cluster_head = p*nodes; %So nut chu
R = 30; %Ban kinh phat toi da ban tin quang ba cua nut chu(m)
Pt = 30; %Cong suat phat ban tin quang ba cua nut chu(mW)
Pl = 1; %Cong suat suy hao do khoang cach(mW/m)
to = 0.001; %Thoi gian lan truyen ban tin tren mot don vi quang duong truyen(s/m)
% Nang luong $
%Nang luong cho ban dau cua mot nut
Eo = 2; %Don vi J
%Nang luong tieu ton de xu ly mot bit trong mot nut
Eelec = 50*10^(-9); %Don vi J/b
%Nang luong bo khuech dai dung de lam viec
Eamp = 100*10^(-12); %Don vi J/b/m^2
%Nang luong dung de tong hop du lieu
EDA = 5*10^(-9); %Don vi J/b
% Kich thuoc goi tin %
k = 4000; %Don vi bit
% Vi tri nut Sink %
xsink = 50;
ysink = 150;
% So chu ky toi da %
r = 2;
% Khoi tao bien so CL %
CLs = 1; %Bien so de tinh toan
% Khoi tao bien dem so nut duoc ket noi voi nut chu cum
con_nodes = 0;
% Khoi tao bien dem so nut khong ket noi dc voi nut chu cum
dis_nodes = 0;
figure('Name','Mang cam bien khong day su dung giao thuc LEACH');
% Thiet lap mang %
h1 = plot(xsink,ysink,'s','LineWidth',2,...
'MarkerEdgeColor','k',...
'MarkerFaceColor','g',...
'MarkerSize',15);
hold on;
for i = 1:nodes
SN(i).id = i; %Dia chi nut mang
SN(i).x = rand*xm; %Hoanh do nut mang
SN(i).y = rand*ym; %Tung do nut mang
SN(i).CL = 0; %Bang 0 thi la nut binh thuong, bang 1 thi la nut chu cum
SN(i).rleft = 0; %Chu ky khong duoc lam nut chu(Thiet lap ban dau)
SN(i).E = Eo; %Nang luong ban dau cua nut
SN(i).S = 1; %Tinh trang ban dau cua nut 1=LIVE 0=DIE
hold on;
h2 = plot(SN(i).x,SN(i).y,'o','LineWidth',2,...
'MarkerEdgeColor','r',...
'MarkerFaceColor','y',...
'MarkerSize',10);
end
% Khoi tao bien tong nang luong tieu ton %
e = 0;
for t = 1:r
dP = zeros(nodes, nodes);
dD = zeros(nodes, nodes);
% Thiet lap cluster head %
for j = 1:nodes
SN(j).g = rand;
if ( SN(j).g < p/(1-p*mod(r,1/p)) ) && ( i ~= SN(j).rleft ) && ( SN(j).S == 1)
CL(j).id = SN(j).id;
CL(j).x = SN(j).x;
CL(j).y = SN(j).y;
% Khoang cach giua nut chu va nut Sink %
CL(j).dS = sqrt((CL(j).x-xsink)^2+(CL(j).y-ysink)^2);
CL(CLs) = CL(j);
SN(j).rleft = 1/p-mod(r,1/p);
SN(j).CL = 1;
SN(j).C = 0;
CLs = CLs+1;
h3 = plot(CL(j).x,CL(j).y,'o','LineWidth',2,...
'MarkerEdgeColor','m',...
'MarkerFaceColor','k',...
'MarkerSize',10);
draw_circle1(SN(j).x,SN(j).y,R,'g');
else SN(j).CL = 0;
end
end
ClusterHeads = CLs-1;
CL=CL(1:ClusterHeads);
disp('So nut chu cum la');
disp(ClusterHeads);
legend('Sink','Sensor Node');
xlabel('Truc hoanh(m)');
ylabel('Truc tung(m)');
title('Mo hinh mang cam bien khong day');
grid on;
% Gop cac node thanh cac cum %
for i = 1:ClusterHeads
for j = 1:nodes
if ( SN(j).CL ~= 1 ) && ( SN(j).S == 1 )
% Khoang cach giua nut binh thuong va cac nut chu cum %
dD(i,j) = sqrt((CL(i).x-SN(j).x)^2+(CL(i).y-SN(j).y)^2);
% Cong suat ban tin quang ba nhan duoc cua nut binh thuong %
dP(i,j) = Pt-Pl*dD(i,j)-10*rand; %Cong suat ton hao do nhieu Pf=10*rand
end
end
end
% Gop cum dua theo Cong suat ban tin quang ba nhan duoc %
for i=1:nodes
%Khoang cach giua nut thuong va nut chu cum SN(i).dD
%Cong suat ban tin quang ba lon nhat ma nut nhan duoc tu nut chu SN(i).dP
%Dia chi nut chu ma nut thuoc cum do SN(i).C
[SN(i).dP, col] = max(dP(:,i));
if (SN(i).CL ~= 1) && (SN(i).dP > 0.1) && ( SN(i).S == 1 )%Cong suat toi thieu de co the nhan duoc la 0.1mW
plot([SN(i).x CL(col).x], [SN(i).y CL(col).y], 'r');
SN(i).C = CL(col).id;
SN(i).dD = dD(col,i);
con_nodes = con_nodes+1;
CL(col).ETx1 = Pt*to*R; %Nang luong phat ban tin quang ba cua nut chu cum
SN(i).ERx = (Eelec+EDA)*k*1/4; %Nang luong nut binh thuong tieu hao thu nhan va xu ly du lieu tu ban tin quang ba
SN(i).ETx1 = Pt*to*dD(col,i); %Nang luong phat ban tin hoi dap cua nut thuong
e = e+SN(i).ERx+SN(i).ETx1;
else
if (SN(i).CL ~= 1)
SN(i).C = 0; %Nut chua thuoc cum nao
dis_nodes = dis_nodes+1;
SN(i).ETx1 = 0;
else
SN(i).C = -1; %Nut chu
SN(i).ETx1 = 0;
end
end
end
for i = 1:ClusterHeads
e = e+CL(i).ETx1;
plot([CL(i).x xsink], [CL(i).y ysink]);
end
disp('So nut duoc ket noi voi nut chu cum:');
disp(con_nodes);
disp('So nut khong duoc ket noi voi nut chu cum:');
disp(dis_nodes);
% Giai doan on dinh %
% Nang luong nut binh thuong tieu hao gui du lieu cho nut chu cum %
for i = 1:nodes
if ( SN(i).CL ~= 1 ) && ( SN(i).S == 1 ) && ( SN(i).C > 0)
SN(i).ETx2 = Eelec*k+Eamp*k*SN(i).dD;
e = e+SN(i).ETx2;
if ( SN(i).E-SN(i).ETx1-SN(i).ETx2-SN(i).ERx ) < 0.2 %Nut chet
SN(i).S = 0;
end
end
end
% Nang luong nut chu cum thu nhan va xu ly du lieu %
for i = 1:ClusterHeads
if ( SN( CL(i).id ).CL == 1 ) && ( SN( CL(i).id ).S == 1 )
CL(i).ERx = (Eelec+EDA)*k;
e = e + CL(i).ERx;
if ( SN( CL(i).id ).E-CL(i).ETx1-CL(i).ERx ) < 0.2 %Nut chet
SN( CL(i).id ).S = 0;
end
end
end
% Nang luong nut chu cum gui du lieu cho nut Sink %
for i = 1:ClusterHeads
if ( SN( CL(i).id ).CL == 1 ) && ( SN( CL(i).id ).S == 1 )
CL(i).ETx2 = (Eelec+EDA)*k + Eamp*k*CL(i).dS;
e = e + CL(i).ETx2;
if ( SN( CL(i).id ).E-CL(i).ETx1-CL(i).ERx-CL(i).ETx2 ) < 0.2 %Nut chet
SN( CL(i).id ).S = 0;
end
end
end
for i = 1:ClusterHeads
SN(CL(i).id).CL = 0;
SN(CL(i).id).C = 0;
CL(i).id = 0;
CL(i).x = 0;
CL(i).y = 0;
CL(i).dS = 0;
end
end

Sign in to comment.

Answers (1)

Bob Thompson
Bob Thompson on 10 May 2019
That is almost guaranteed to mean that 'i' is not a positive integer, or, more likely, that CL(i).id is not a positive integer. Double check that you don't have a negative, 0, empty, or other non-positive integer value for CL(i).id.
  2 Comments
Bob Thompson
Bob Thompson on 13 May 2019
'I tried to fix it but I can't'
What does this mean? What did you do? What results did you get? Are you still receiving the same error message?
Double check that you don't have a negative, 0, empty, or other non-positive integer value for CL(i).id
Did you try this? What results do you get for CL(i).id, or just i for that matter?

Sign in to comment.

Categories

Find more on Guidance, Navigation, and Control (GNC) in Help Center and File Exchange

Products


Release

R2013a

Community Treasure Hunt

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

Start Hunting!