How do I resolve the Index exceeds array elements?
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
Jan
2022-3-15
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
2023-6-28
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.
回答(1 个)
Keerthi Reddy
2023-6-30
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.
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!