フィルターのクリア

How do I resolve the Index exceeds array elements?

4 ビュー (過去 30 日間)
Madhura Ramani
Madhura Ramani 2022 年 3 月 15 日
回答済み: Keerthi Reddy 2023 年 6 月 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
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
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
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.

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by