Clear Filters
Clear Filters

How do I resolve the Index exceeds array elements?

4 views (last 30 days)
Hi,
I am having trouble resolving this Index exceeds number of array elements. I am processing two images of different array elements and I am getting the error of Index exceeding the array elements. Can you help me fix the error and why is it happening?
%---FOR EVERY CELL GO FROM VERTEX TO VERTEX AND COUNT PIXELS AND DISTANCES--
failed=[];
for i=1:numel(PPm)
[~, index]=sort(angle(complex(v{i}(:,1)-XXm(i), v{i}(:,2)-YYm(i))));
Indexi{i}=[index; index(1)]';
Vertex{i}(:,1)=v{i}(Indexi{i},1); Vertex{i}(:,2)=v{i}(Indexi{i},2); %now they are sorted clockwise
for zlj=1:numel(index)
Edges(zlj)=pdist2([Vertex{i}(zlj,1) Vertex{i}(zlj,2)], [Vertex{i}(zlj+1,1), Vertex{i}(zlj+1,2)]);
end
Mean_edge_streight(i)=mean(Edges);
Std_edge_streight(i)=std(Edges);
clear Edges
number_of_pixels=0;
it_was_wrong_direction=0;
Numb=numel(Vertex{i}(:,1));
zlj=0; NUM_PIXELS_EDGE=0;
for Try_it=1:Numb*2
if it_was_wrong_direction==0
zlj=zlj+1;
end
Thank you in advance..
  2 Comments
Jan
Jan on 15 Mar 2022
The code cannot run dues to the missing end statements. You did not reveal the important detail, in which line the error occurs. Whenever you mention an error in the forum, attach the compelete message. The details matter.
Can you provide some input data, e.g. created by rand?
This time I've edited your message and applied a proper formatting. Use the tools on top of the edit field to mark code as code. Thanks.
Shree Charan
Shree Charan on 28 Jun 2023
Hey @Madhura Ramani, "Index exceeds number of array elements" occurs when a variable is indexed at a position higher than the length of the array. It might be useful to share the values of variables so as to repoduce the error.

Sign in to comment.

Answers (1)

Keerthi Reddy
Keerthi Reddy on 30 Jun 2023
Hi Madhura,
The error "Index exceeds number of array elements" occurs when you try to access an element in an array using an index that is greater than the number of elements in the array. From the above code snippet which you have mentioned, this error is likely happening in the following line:
Edges(zlj)=pdist2([Vertex{i}(zlj,1) Vertex{i}(zlj,2)], [Vertex{i}(zlj+1,1), Vertex{i}(zlj+1,2)]);
This error is occurring because the variable Edges is not pre-allocated with a fixed size, and you are trying to access elements beyond the current size of the array.
Hope this helps.

Categories

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

Community Treasure Hunt

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

Start Hunting!