Selecting specific values from an array based on a defined condition

조회 수: 25 (최근 30일)
SB
SB 2019년 9월 25일
댓글: SB 2019년 9월 25일
I have the following array:
d = [0,100,-100];
m = 300;
I want to select certain values of array d as follows:
d(0<=m+d<=300);
So i want to select all the d values where m+d is beween 0 and 300 with both ends included. The result should look like [0,-100] and 100 should be excluded since it is out the defined range. However, I am getting d to be [0,100,-100]. Please let me know where I went wrong.

채택된 답변

Fabio Freschi
Fabio Freschi 2019년 9월 25일
편집: Fabio Freschi 2019년 9월 25일
d+m is not scalar: use a single & for logical AND
d(d+m >= 0 & d+m <= 300)

추가 답변 (1개)

Johannes Fischer
Johannes Fischer 2019년 9월 25일
편집: Johannes Fischer 2019년 9월 25일
Th logical expression
0 <= m+d <= 300
is not interpreted by Matlab as you think it is. It rather is interpreted as
(0 <= m+d) <= 300
where the expression in brackets results in logical 0 and 1 values which all are below 300, which in turn results in logical 1 at each position. What you want is a combination of both cases
d(0 <= m+d && m+d <= 300);
  댓글 수: 3
Johannes Fischer
Johannes Fischer 2019년 9월 25일
You are right, only one '&' is necessary
d(0 <= m+d & m+d <= 300);

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

제품


릴리스

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by