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