matrix

2 ビュー (過去 30 日間)
Mate 2u
Mate 2u 2012 年 6 月 14 日
Hi I have a 1 x 100 row matrix. I want to calculate in one line what percentage (interms of decimal) are the elements which are the number 1 (matrix is full of 1's, -1's and 0s'.)
Then I would like another one line to give the percentage of -1's.

採用された回答

Wayne King
Wayne King 2012 年 6 月 14 日
x = randi([-1 1],1,100);
percent1 = 100*(length(x(x==1))/numel(x));
percentminus1 = 100*(length(x(x == -1))/numel(x));
Another way
sum(bsxfun(@eq,x,1))
sum(bsxfun(@eq,x,-1))

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2012 年 6 月 14 日
mean(YourArray == 1)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by