A way to write "if x is in y matrix do this"
조회 수: 35 (최근 30일)
이전 댓글 표시
so long story short im attempting to create a random value and then see if the value is in one range of numbers or another.
ive been able to do the randomising numbers part and tried to use the "ismember" function in the form:
if ismember(y,x)
im pretty certain this is the part of the code that is stopping me from getting the answers i want but cant find anything to solve it
댓글 수: 0
채택된 답변
Star Strider
2023년 1월 30일
It depends what the random number is, and how you define ‘y’ . The ismember function works for exact comparisons. The ismembertol function allows tolerances, and so is suitable for floating-point numbers.
y = 1:10;
x = randi(20,1)
im = ismember(y,x)
Lany = any(im)
Lall = all(im)
Experiment to get the result you want.
.
댓글 수: 0
추가 답변 (1개)
John D'Errico
2023년 1월 30일
편집: John D'Errico
2023년 1월 30일
You CANNOT use ismember to test if a number is in an interval. Even ismembertol does not do that. Using code to do something it is not programmed to do tends to result in failures.
Can you test to see if a number lies in some interval? OF COURSE!!!!!!! That part is trivial.
intervaltest = @(x,lb,ub) (x>=lb) & (x<=ub);
x = rand(1,6)
intervaltest(x,.3,.6)
So points 1,3,4,5 were in the interval, and points 2 and 6 were not.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!