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

4 次查看(过去 30 天)
What is meant By:
Subscript indices must either be real positive integers or logicals.
  1 个评论
Les Beckham
Les Beckham 2020-7-8
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:

请先登录,再进行评论。

采纳的回答

KSSV
KSSV 2020-7-8
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 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by