What is the meaning of the Error Mesage "Subscript indices must either be real positive integers or logicals."
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
What is meant By:
Subscript indices must either be real positive integers or logicals.
1 commentaire
Les Beckham
le 8 Juil 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:
Réponse acceptée
KSSV
le 8 Juil 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
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Logical dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!