Average of a single element in an array

1 回表示 (過去 30 日間)
jawaher shah
jawaher shah 2019 年 3 月 13 日
コメント済み: jawaher shah 2019 年 3 月 13 日
Hello,
Im generating random numbers between (1 and 2) in 2D array. I want to get the average times of getting number (1) in the array. i tried to use M = mean(1,'all') and M = mean(1) but it didnt work out.
Thanks

採用された回答

Rik
Rik 2019 年 3 月 13 日
The best method depends on how you have generated your array. I will show two examples below.
%example 1: only integers
A=randi(2,200,200);
fraction_of_ones= sum(A==1,'all')/numel(A);
%example 2: non-integers as well
A=randn(200,200)+1.5;A(A<1)=1;A(A>2)=2;
tolerance=1e-3;
fraction_of_ones= sum(abs(A-1)<=tolerance,'all')/numel(A);
  1 件のコメント
jawaher shah
jawaher shah 2019 年 3 月 13 日
Thank you so much it really helped me =D

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by