apply condition on Matrix?
이전 댓글 표시
if i have a matrix B=[1111000] and another integer T=4 i want to apply while loop if number of One's in B>=T how should i write it to get desire condition
댓글 수: 12
madhan ravi
2019년 9월 9일
편집: madhan ravi
2019년 9월 9일
What have you tried for your homework?
Ali Mukhtar
2019년 9월 9일
madhan ravi
2019년 9월 9일
편집: madhan ravi
2019년 9월 9일
Post the code that you tried.
youcha
2019년 9월 9일
Can you please explain more of what do you want to do with your while loop? It should be in a form of : I want to do action B while the condition A is true. Your condition in this ase is B>=T while your action is not clear
Ali Mukhtar
2019년 9월 9일
Ali Mukhtar
2019년 9월 9일
madhan ravi
2019년 9월 9일
hint: If one add one or else continue
Ali Mukhtar
2019년 9월 9일
madhan ravi
2019년 9월 9일
Your goal is to count the number of ones in B and then finally check if it's greater than or equal to T , is that right?
Ali Mukhtar
2019년 9월 9일
madhan ravi
2019년 9월 9일
Why not use a for loop? Is loop necessary?
Ali Mukhtar
2019년 9월 9일
채택된 답변
추가 답변 (1개)
David Hill
2019년 9월 9일
If B is a 1 by x vector of 1's and 0's (B = [1,1,1,1,1,0,0,0,0,0])
while sum(B)>T
if B is a 1 by x character array (B = '1111100000')
while sum(double(B)-48)>T
Your matrix B is not described well above, it looks like a single number.
댓글 수: 3
Ali Mukhtar
2019년 9월 9일
David Hill
2019년 9월 9일
If your array is a character array '111110000', then I converted to a number array which is a ascii representative of each character and subtract 48=='0' to get an array of 1's and 0's. If you already have an array of 1's and 0's you don't need to do this.
Ali Mukhtar
2019년 9월 9일
카테고리
도움말 센터 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!