apply condition on Matrix?

7 次查看(过去 30 天)
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
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
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 个评论
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
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 个评论
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Operating on Diagonal Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by