Index exceeds the number of array error
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Sashi Mendoza
el 31 de Mzo. de 2022
Editada: Sashi Mendoza
el 3 de Abr. de 2022
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?
Respuesta aceptada
Arif Hoq
el 31 de Mzo. de 2022
Editada: Arif Hoq
el 31 de Mzo. de 2022
var = randi(100,20,20);
[val,idx] = max(var(:))
[x, y] = find(var == val)
idx2=2;
x0 = x(idx2)
y0 = y(idx2)
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(:))
[x, y] = find(var == val)
% idx2=2;
x0 = x(idx)
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 comentarios
Arif Hoq
el 31 de Mzo. de 2022
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)
idx or index extract the value from the position 631. if you notice in x the 631th value is 1261.
Stephen23
el 31 de Mzo. de 2022
"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)
But just like any other indexing, those indices must be within the size of that (scalar) array.... which IDX is not.
Más respuestas (0)
Ver también
Categorías
Más información sobre Matrix Indexing en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!