Calculating standard deviation of matrix

15 views (last 30 days)
Ralph
Ralph on 31 Aug 2020
Edited: Ralph on 9 Sep 2020
ADS = audioDatastore(folder);
while hasdata(ADS)
x=read(ADS);
P=8; % no of neighbours for computation
b_right=zeros(1,4); % variables for computing differnces on sides
b_left=b_right;
[row,c]=size(x); % total number of samples is "row"
total_frames=row/9;% number of total frames as 1 Frames=9 samples
% so total frames = total samples/ frame size
% full_frame=zeros(9,500);
h=row/9;
g=std(h, 'all');
So I want to calculate standard deviation of each sample in a frame. There are 9 samples in each frame. and want to show the result in g. Currently g is returning 0 value. I want my code to compute the standard deviation of each sample in single frame.

Accepted Answer

Dana
Dana on 31 Aug 2020
I'm not sure I quite understand what you want, but a couple of things I see. First, row is a scalar giving the number of rows in z, and therefore h=row/9 is also just a scalar. In that case, g=std(h) is the standard deviation of a set made up of the single number h, which, by convention, equals 0.
I'm guessing you really want to compute standard deviations of sub-groups of the elements of x. Is x a vector or a matrix (i.e., is c=1 or >1)?
If x is a vector, and you want to compute standard deviations of each successive, non-overlapping group of 9 elements of x, then if the number of elements of x is an even multiple of 9 (i.e., if h=row/9 is an integer), you can do this easily via
x2 = reshape(x,9,h); % reshape the vector x into a matrix with 9 rows and h columns
g = std(x2,1); % compute the standard deviation of each column
If h is not an integer, it means your last frame will have less than 9 samples in it. You have to figure out how you want to deal with that. For example, you may want to compute standard deviations only for frames with 9 elements in them, in which case:
flh = floor(h); % round h downward
x2 = reshape(x(1:9*flh),9,flh); % reshape the first 9*flh elements of x into a 9-by-flh matrix
g = std(x2,1); % compute the standard deviation of each column
If x is a matrix, then you need to provide some more detail about exactly what you're computing the standard deviation of.
  2 Comments
Ralph
Ralph on 1 Sep 2020
Thank you soo much, your answer is really appreciated. Yes x is a matrix.
Dana
Dana on 1 Sep 2020
As I said in my previous post, "If x is a matrix, then you need to provide some more detail about exactly what you're computing the standard deviation of." So if you want additional help, you'll have to clarify the problem you're trying to solve here.

Sign in to comment.

More Answers (0)

Categories

Find more on Data Distribution Plots 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!