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
madhan ravi 2019 年 9 月 9 日
編集済み: madhan ravi 2019 年 9 月 9 日
What have you tried for your homework?
Ali Mukhtar
Ali Mukhtar 2019 年 9 月 9 日
i apply a for loop and put while condition until B matrix is have 1's but that is not correct
madhan ravi
madhan ravi 2019 年 9 月 9 日
編集済み: madhan ravi 2019 年 9 月 9 日
Post the code that you tried.
youcha
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
Ali Mukhtar 2019 年 9 月 9 日
if i can count the number of 1's in B matrix i can apply the condition
Ali Mukhtar
Ali Mukhtar 2019 年 9 月 9 日
for i=1
for j=1:n %n= number of one in matrix B
while B(i,j)>=T
end
madhan ravi
madhan ravi 2019 年 9 月 9 日
hint: If one add one or else continue
Ali Mukhtar
Ali Mukhtar 2019 年 9 月 9 日
dnt understand in which one i have to add one? can i count number of 1's in B matrix?
madhan ravi
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
Ali Mukhtar 2019 年 9 月 9 日
yes exactly
madhan ravi
madhan ravi 2019 年 9 月 9 日
Why not use a for loop? Is loop necessary?
Ali Mukhtar
Ali Mukhtar 2019 年 9 月 9 日
not it is not neccesary this was last what i tried so sent it to you

サインインしてコメントする。

 採用された回答

madhan ravi
madhan ravi 2019 年 9 月 9 日
編集済み: madhan ravi 2019 年 9 月 9 日

1 投票

Just use logical indexing "==" to see the values equal 1 and use nnz() and then use >= T if you get 1 it's true else false.
help ==
help nnz
help >=

5 件のコメント

Ali Mukhtar
Ali Mukhtar 2019 年 9 月 9 日
nnz()?
madhan ravi
madhan ravi 2019 年 9 月 9 日
help nnz % will tell you what it actually does [hint: this is to count the no of ones present in B]
Ali Mukhtar
Ali Mukhtar 2019 年 9 月 9 日
can you please write few code lines it become more and more complex
madhan ravi
madhan ravi 2019 年 9 月 9 日
nnz(B==1) >= T
%^^^^^^^^--- counts the number of ones
Ali Mukhtar
Ali Mukhtar 2019 年 9 月 9 日
we can use it with while loop
while or if nnz(B==1)>= T ?

サインインしてコメントする。

その他の回答 (1 件)

David Hill
David Hill 2019 年 9 月 9 日

0 投票

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
Ali Mukhtar 2019 年 9 月 9 日
while sum(double(B)-48)>T
B is array which contains 1's and 0's . in above code line can you explain why you use 48 and subtract it from B
David Hill
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
Ali Mukhtar 2019 年 9 月 9 日
but i dnt know if my value is exactly 111110000 so subtracting it from 48 is not fesible . if you show result by compiling it i can use it also

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by