"index exceeds matrix dimension" error. generated when I try to implement APIT localization algorithm for wireless sensor networks. Please i need your help dear matlab users to fix this error..

1 次查看(过去 30 天)
function APIT(grid_length) % Approximate Point In Triangle (APIT) Localization Algorithm
load '../Deploy Nodes/coordinates.mat';
load '../Topology Of WSN/neighbor.mat';
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%all_nodes = anchor_nodes+unknown_nodes;
unknown_node_index = all_nodes.anchors_n+1:all_nodes.nodes_n;
row_n=ceil(all_nodes.square_L/grid_length);col_n=row_n;
centroid_x =((1:col_n)-0.5)*grid_length;
centroid_y= ((1:row_n)-0.5)*grid_length;
for i=unknown_node_index
disp([num2str(i)]);
neighboring_anchor_index=find(neighbor_matrix(i,1:all_nodes.anchors_n)==1);
neighboring_anchor_n=length(neighboring_anchor_index);
if neighboring_anchor_n==3
gridmap=zeros(row_n,col_n);
grid_covered_flag=zeros(row_n,col_n);
for a=1:neighboring_anchor_n-2
for b=a+1:neighboring_anchor_n-1
for c=b+1:neighboring_anchor_n
neighboring_node_index=setdiff(find(neighbor_matrix(i,:)==1),neighboring_anchor_index([a b c]));
perimeter(sqrt((neighboring_anchor_n(1,1)-neighboring_anchor_n(2,1)).^2+(neighboring_anchor_n(1,2)-neighboring_anchor_n(2,2)).^2), sqrt((neighboring_anchor_n(1,1)-neighboring_anchor_n(3,1)).^2+(neighboring_anchor_n(1,2)-neighboring_anchor_n(3,2)).^2), sqrt((neighboring_anchor_n(2,1)-neighboring_anchor_n(3,1)).^2+(neighboring_anchor_n(2,2)-neighboring_anchor_n(3,2)).^2)); % The error message shows "index exceeds matrix dimension" error happen in this line.
if perimeter > rss Grid_in_triangle_abc=inpolygon(centroid_x,centroid_y,all_nodes.estimated(neighboring_anchor_index([a b c]),1),all_nodes.estimated(neighboring_anchor_index([a b c]),2));%Grid covered by triangles abc
gridmap=gridmap+Grid_in_triangle_abc;
end
grid_covered_flag=grid_covered_flag|Grid_in_triangle_abc;
end
end
if any(any(grid_covered_flag))
weight_max=max(max(gridmap(grid_covered_flag)));
weight_max_index=intersect(find(gridmap==weight_max),find(grid_covered_flag==1));
[weight_max_ind_row,weight_max_ind_col]=ind2sub(size(gridmap),weight_max_index);
all_nodes.estimated(i,:)=mean([weight_max_ind_col weight_max_ind_row;weight_max_ind_col weight_max_ind_row]*grid_length-0.5*grid_length);
all_nodes.anc_flag(i)=2;
end
save '../Localization Error/result.mat' all_nodes comm_r;
end
end
end
end
Thank you very much in advance.
  2 个评论
Image Analyst
Image Analyst 2018-5-13
kelil, here's a tip to format your code correctly. In MATLAB type control-a control-i to properly indent your code. Then copy it and paste it here. Highlight the code and then click the {}Code button. See this link
You forgot to attach your .mat files and forgot to tell us what you passed in for grid_length so we can't run your code. You also forgot to tell us the complete error so we don't know what line of code threw an error. Please read this link first then paste all the red text here, and attach the .mat files, so we can help you.
Kelil Mohammed
Kelil Mohammed 2018-5-17
Thank you very much for your response and comments. Here I modify input argument but still after running few the same error occurred
function APIT(all_nodes)
load 'coordinates.mat';
load 'neighbor.mat';
%all_nodes = anchor_nodes+unknown_nodes;
disp('Longer time, wait patiently...');
unknown_node_index = all_nodes.anchors_n+1:all_nodes.nodes_n;
row_n=ceil(all_nodes.square_L);
col_n=row_n;
centroid_x=repmat(([1:col_n]-0.5),row_n,1);
centroid_y=repmat(transpose([1:row_n]-0.5),1,col_n);
for i=unknown_node_index
disp([num2str(i),':I am running, donot press it ...']);
neighboring_anchor_index=find(neighbor_matrix(i,1:all_nodes.anchors_n)==1);
neighboring_anchor_n=length(neighboring_anchor_index);
if neighboring_anchor_n==3
gridmap=zeros(row_n,col_n);
grid_covered_flag=zeros(row_n,col_n);
for a=1:neighboring_anchor_n-2
for b=a+1:neighboring_anchor_n-1
for c=b+1:neighboring_anchor_n
neighboring_node_index=setdiff(find(neighbor_matrix(i,:)==1),neighboring_anchor_index([a b c]));
perimeter(sqrt((neighboring_anchor_n(1,1)-neighboring_anchor_n(2,1)).^2+(neighboring_anchor_n(1,2)-neighboring_anchor_n(2,2)).^2), sqrt((neighboring_anchor_n(1,1)-neighboring_anchor_n(3,1)).^2+(neighboring_anchor_n(1,2)-neighboring_anchor_n(3,2)).^2), sqrt((neighboring_anchor_n(2,1)-neighboring_anchor_n(3,1)).^2+(neighboring_anchor_n(2,2)-neighboring_anchor_n(3,2)).^2));
if perimeter > rss
Grid_in_triangle_abc=inpolygon(centroid_x,centroid_y,all_nodes.estimated(neighboring_anchor_index([a b c]),1),all_nodes.estimated(neighboring_anchor_index([a b c]),2));%Grid covered by triangles abc
gridmap=gridmap+Grid_in_triangle_abc;
end
grid_covered_flag=grid_covered_flag|Grid_in_triangle_abc;
end
end
if any(any(grid_covered_flag))
weight_max=max(max(gridmap(grid_covered_flag)));
weight_max_index=intersect(find(gridmap==weight_max),find(grid_covered_flag==1));
[weight_max_ind_row,weight_max_ind_col]=ind2sub(size(gridmap),weight_max_index);
all_nodes.estimated(i,:)=mean([weight_max_ind_col weight_max_ind_row;weight_max_ind_col weight_max_ind_row]*grid_length-0.5*grid_length);
all_nodes.anc_flag(i)=2;
end
end
end
end
end
The errors generated as follows:
Index exceeds matrix dimensions.
Error in APIT (line 30)
perimeter(sqrt((neighboring_anchor_n(1,1)-neighboring_anchor_n(2,1)).^2+(neighboring_anchor_n(1,2)-neighboring_anchor_n(2,2)).^2),
sqrt((neighboring_anchor_n(1,1)-neighboring_anchor_n(3,1)).^2+(neighboring_anchor_n(1,2)-neighboring_anchor_n(3,2)).^2),
sqrt((neighboring_anchor_n(2,1)-neighboring_anchor_n(3,1)).^2+(neighboring_anchor_n(2,2)-neighboring_anchor_n(3,2)).^2));
Here are my mat files.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by