How to fix " Index exceeds matrix dimension" error?
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Kelil Mohammed
le 17 Mai 2018
Commenté : Kelil Mohammed
le 18 Mai 2018
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 commentaires
Réponse acceptée
Stephen23
le 17 Mai 2018
Modifié(e) : Stephen23
le 17 Mai 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 commentaires
Stephen23
le 18 Mai 2018
Modifié(e) : Stephen23
le 18 Mai 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.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Matrices and Arrays dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!