Index exceeds the number of array error

조회 수: 7 (최근 30일)
Sashi Mendoza
Sashi Mendoza 2022년 3월 31일
편집: Sashi Mendoza 2022년 4월 3일
a = a certain 20x20 matrix;
[val,idx] = max(a(:));
[x, y] = find(a == val);
At the underlined part of the code, this displays the error "Index exceeds the number of array elements (1)". How to fix this?
  댓글 수: 3
Chunru
Chunru 2022년 3월 31일
Then x is a scaler and you cannot find x(idx) if idx is greater than 1.
Stephen23
Stephen23 2022년 3월 31일
"x = 11 and y = 21 though"
And why do you expect that IDX should be able to index into scalar numerics?

댓글을 달려면 로그인하십시오.

채택된 답변

Arif Hoq
Arif Hoq 2022년 3월 31일
편집: Arif Hoq 2022년 3월 31일
var = randi(100,20,20);
[val,idx] = max(var(:))
val = 100
idx = 15
[x, y] = find(var == val)
x = 7×1
15 5 15 14 16 7 14
y = 7×1
1 5 7 13 19 20 20
idx2=2;
x0 = x(idx2)
x0 = 5
y0 = y(idx2)
y0 = 5
here your idx=2(intentionally fixed) and your number of element of x and y is 7. so x0 and y0 can extract from index 2. now see the below example
%%
var = randi(100,20,20);
[val,idx] = max(var(:))
val = 100
idx = 18
[x, y] = find(var == val)
x = 5×1
18 16 7 4 10
y = 5×1
1 4 8 9 14
% idx2=2;
x0 = x(idx)
Index exceeds the number of array elements. Index must not exceed 5.
y0 = y(idx)
here your idx=18, but your number of element of x and y is 5.so it can not extract form index 18. therefore, you are getting error.
  댓글 수: 2
Arif Hoq
Arif Hoq 2022년 3월 31일
as Stephen said already you can not specify index in a sclar value. you need a range of element.
x=1:2:1550;
idx = 631;
x0=x(idx)
x0 = 1261
idx or index extract the value from the position 631. if you notice in x the 631th value is 1261.
Stephen23
Stephen23 2022년 3월 31일
"Stephen said already you can not specify index in a sclar value"
It is certainly possible to index into a scalar value:
A = 6;
A(1,1)
ans = 6
But just like any other indexing, those indices must be within the size of that (scalar) array.... which IDX is not.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by