Clear Filters
Clear Filters

How to fix " Index exceeds matrix dimension" error?

2 views (last 30 days)
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

Accepted Answer

Stephen23
Stephen23 on 17 May 2018
Edited: Stephen23 on 17 May 2018
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 Comments
Stephen23
Stephen23 on 18 May 2018
Edited: Stephen23 on 18 May 2018
@Kelil Mohammed: I have no idea. You have not explained anything about your code nor written any code comments. It is not clear what you expect to happen by accessing non-existent elements of a scalar, so I have no idea what you are trying to do.
Kelil Mohammed
Kelil Mohammed on 18 May 2018
Thank you Sir for your response. I want to find perimeter of a virtual triangle formed by three anchor nodes say anchor node A,B and C. say if coordinates are A(xa,ya),B(xb,yb) and C(xc,yc) and this is what I tried on line 30. Sir if you get my idea modify line 30 and explain for me. Thanks.

Sign in to comment.

More Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!