I want to calculate the RMS, MEAN, STD of the first three elements of any predefine array and store it in new array and then calculate the same for the next three elements

조회 수: 3 (최근 30일)
I want to calculate the RMS, MEAN, STD of the first three elements of any predefine array and store it in new array and then calculate the same for the next three elements and store it in the same new array and so on till last.

답변 (1개)

dpb
dpb 2022년 6월 9일
편집: dpb 2022년 6월 9일
N=size(x,1); % number rows in x
g=kron([1:N/3].',ones(3,1)); % grouping variable by three
[gmean,gstd,grms]=grpstats(x,g,{'mean','std',@rms})); % compute
See grpstats for all the details of possible ways to organize output.
NB: the above requires N be an even multiple of 3
  댓글 수: 2
Rajeev Kumar
Rajeev Kumar 2022년 6월 10일
Please make changes in my code with reference to the above query
clear all
clc
B=[1,20,50,11,15,56,15,58,45,47,65,42,67,98,53,48,47,78,65,25];
A=[];
D=[];
% mean=0;
count=1;
l=length(B);
for i=1:l
mean=std(B);
if count ==3
A=[A,mean];
mean=0;
end
if count == 3
count=1;
else
count=count+1;
end
disp(A);
end
dpb
dpb 2022년 6월 10일
편집: dpb 2022년 6월 10일
Use "the MATLAB way" I showed you instead.
"x" is your "predefined array"; simply use your variable name. You'll want to add error checking or otherwise handle the case of other than a multiple of three records if it is ever possible that condition can arise; we don't have sufficient information to know about edge case.
You can use/rearrange the returned statistics arrays with whatever variable structure/names you wish for the end result, but compute the results efficiently with grouping a variable.
mean=std(B);
Don't use mean as a variable; it's the builtin function for the statistic; doing this will create all sorts of unexpected havoc.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Workspace Variables and MAT-Files에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by