Average of a single element in an array

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 日

1 投票

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 件)

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

質問済み:

2019 年 3 月 13 日

コメント済み:

2019 年 3 月 13 日

Community Treasure Hunt

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

Start Hunting!

Translated by