What is the meaning of the Error Mesage "Subscript indices must either be real positive integers or logicals."

1 visualización (últimos 30 días)
What is meant By:
Subscript indices must either be real positive integers or logicals.
  1 comentario
Les Beckham
Les Beckham el 8 de Jul. de 2020
The error message seems pretty clear. You cannot use an index into a matrix that is not either a real positive integer (or an array of those) or a logical (or an array of zeros and ones).
For example,
A(-1) is an error while A(1) is fine (assuming that A is defined already).
Logical indexing involves using an array of zeros and ones that specify which array elements to select (one to select, zero to ignore).
For example, if A = [0 1 2 3 4],
A(logical([0 1 0 1 0])) will be equal to [1 3].
I suggest that you read the documentation:

Iniciar sesión para comentar.

Respuesta aceptada

KSSV
KSSV el 8 de Jul. de 2020
In MATLAB the indices of an array always should be positive integers. The indices can be 0,1 if it is og logical type.
Example:
A = rand(1,10) ;
A(1) % no error as 1 is positve
A(0) % error as 0 is not allowed
A(-1) % error as negative indices not allowed
Logicals:
A = rand(1,20) ;
idx = A>0.5 ; % idx is logical indexing with 0, 1
class(idx) % it says logical
idx % it has 0, 1
A(idx) % logical indexing works
id = [0 1 0 1];
class(id) % double
A(id) % error, as indices are double
id = logical(id) ; % convert double to logical
A(id) % no error

Más respuestas (0)

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by