How to fix " Index exceeds matrix dimension" error?
2 次查看(过去 30 天)
显示 更早的评论
I want to implement APIT (Approximate-Point-In_Triangle) localization algorithm for wireless sensor networks and some errors generated.here is my code.
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));
My mat files, APIT and Perimter codes are attached
0 个评论
采纳的回答
Stephen23
2018-5-17
编辑:Stephen23
2018-5-17
You define the variable neighboring_anchor_n on line 21, with this:
neighboring_anchor_n=length(neighboring_anchor_index);
length always returns a scalar. Then on line 30 you try to access lots of elements of neighboring_anchor_n that simply do not exist (because it is scalar):
...neighboring_anchor_n(1,1)-neighboring_anchor_n(2,1)...
...neighboring_anchor_n(1,2)-neighboring_anchor_n(2,2)...
...
...neighboring_anchor_n(2,2)-neighboring_anchor_n(3,2)...
3 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!