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 Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by