How index using inequalities

조회 수: 5 (최근 30일)
Danny Maefengea
Danny Maefengea 2020년 6월 21일
댓글: Danny Maefengea 2020년 6월 22일
Hello guys, I was doing this exercise and when I tried running my script, errors appeared.
Create a 1*100 vector A with random numbers from 0 to 10
A = randi([0,10],1,100)
Display all the numbers in A that are greater than 5
u = disp(A>5)
Delete all the numbers in A that are greater than 5
u = []

채택된 답변

Walter Roberson
Walter Roberson 2020년 6월 22일
A>5 creates a logical vector, with each entry being true if the corresponding A was (strictly) greater than 5, and false if the corresponding A was less than or equal to 5.
disp(A>5) would display that logical vector.
To display the contents of A where A > 5 you need to use A>5 to select entries out of A using logical indexing.
SomeArray(logical_mask)
However... disp() does not return any value, and it is an error to attempt to assign disp() to a variable.
u = []
If you had assigned u the parts of A that are greater than 5, then u = [] would just empty out that array, without changing A. You again want to use logical indexing
SomeArray(logical_mask) = []
  댓글 수: 3
Walter Roberson
Walter Roberson 2020년 6월 22일
Well you will need to define the logical_mask as well.
Danny Maefengea
Danny Maefengea 2020년 6월 22일
oh okay thanks

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

추가 답변 (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