0
votes

Hello I am new to programming in Matlab and am attempting to find the number of data points within a set of data that are x standard deviations away from the mean. The dataset is 5,000 random numbers using randn. I would like to do this with a loop, and I think the steps are should take are as follows:

  1. Have a loop go through the 5,000 random data points
  2. Count the points that are +- 1 std from the mean
  3. Print the number of points

I am not really sure where to begin and if someone could point me in the right direction it would be greatly appreciated. Thanks.

1
Here's the code that tells you how many elements from the array a are between lim1 and lim2: sum(a>=lim1 & a<=lim2). Now adapt that to an array, with mean and standard deviation.user2271770
There is a function to calculate the standard derivation, no need for loops or similar. Check the documentation for stdDaniel
Take a look at this question, it is very similar. Instead of counting, it is about deleting, but CST-Link already explained to you how to count.Daniel

1 Answers

0
votes
N = 5000;         % Number of data points
x = randn(N,1);   % Random vector
mu = mean(x);     % Mean of vector
sig = std(x);     % Stan. dev. of vector

% This is a logical array that signifies where the
% condition is true.
inds = (x >= (mu - sig)) & (x <= (mu + sig));

Can you take it from here?