How can I use conditional operators to only obtain values from an array which satisfies the condition?
    조회 수: 5 (최근 30일)
  
       이전 댓글 표시
    
I have an array A which contains a list of numbers. I have an array m which is empty at the start. 
When the for loop is executed, a random number from array A is chosen and assigned to variable B. The number associated with variable B is added to the empty array m and the process repeats 10 times. 
What I want to do is have an array which only contains the values which satisfy the condition includes number that are lower than 5 and higher than 14 (non-inclusive). I tried using conditional operators but it does not seem to be working the final_result variable always ends up with an empty array, even on the occasion where there are numbers in the m array which satisy the conditions specified. Is there any other way I can do this?
A = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16];
m = [];
    for i = 1:10
        B = randsample(A,1);  
        m(end+1) = B;  
    end
 final_result = m(m < 5 & m > 14);
댓글 수: 1
  Simon Chan
      
 2021년 8월 24일
				I think you are looking for numbers that are lower than 5 OR higher than 14.
답변 (1개)
  Srijith Kasaragod
    
 2021년 8월 30일
        As per my understanding, final result holds values in m that are less than 5 or greater than 14. Executing '&' operation will return zero elements as no number can be both less than 5 and greater than 14. Specifying final_result as the following will give the required output:
final_result= m(m < 5 | m > 14)
댓글 수: 0
참고 항목
카테고리
				Help Center 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


