Average of a single element in an array
1 view (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
Rik
on 13 Mar 2019
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);
More Answers (0)
See Also
Categories
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!