Average of a repeating array
조회 수: 6 (최근 30일)
이전 댓글 표시
Hi,
I've got an output vector from simulink that has dimensions of 25560x1. There are 100 individual cycles within this data and I am trying to get an average of all of them. I am using the attached code to isolate each 256 cycle element, add it together, and then divide to achieve the average. However, I keep getting an error about exceeding array elements. Is there a better way to "squash" this 25560 array into a 256 array with the data being averaged? Thank you.
Bat10Count = round(25560/100);
for x = 1:Bat10Count
TotalBatteryP10_293(x)=(BatteryS10_293(x)+BatteryS10_293(Bat10Count+x)+BatteryS10_293(2*Bat10Count+x)+BatteryS10_293(3*Bat10Count+x)+BatteryS10_293(4*Bat10Count+x)+BatteryS10_293(5*Bat10Count+x)+BatteryS10_293(6*Bat10Count+x)+BatteryS10_293(7*Bat10Count+x)+BatteryS10_293(8*Bat10Count+x)+BatteryS10_293(9*Bat10Count+x)+ BatteryS10_293(10*Bat10Count+x)+BatteryS10_293(11*Bat10Count+x)+BatteryS10_293(12*Bat10Count+x)+BatteryS10_293(13*Bat10Count+x)+BatteryS10_293(14*Bat10Count+x)+BatteryS10_293(15*Bat10Count+x)+BatteryS10_293(16*Bat10Count+x)+BatteryS10_293(17*Bat10Count+x)+BatteryS10_293(18*Bat10Count+x)+BatteryS10_293(19*Bat10Count+x)+BatteryS10_293(20*Bat10Count+x)+BatteryS10_293(21*Bat10Count+x)+BatteryS10_293(22*Bat10Count+x)+BatteryS10_293(23*Bat10Count+x)+BatteryS10_293(24*Bat10Count+x)+BatteryS10_293(25*Bat10Count+x)+BatteryS10_293(26*Bat10Count+x)+BatteryS10_293(27*Bat10Count+x)+BatteryS10_293(28*Bat10Count+x)+BatteryS10_293(29*Bat10Count+x)+BatteryS10_293(30*Bat10Count+x)+BatteryS10_293(31*Bat10Count+x)+BatteryS10_293(32*Bat10Count+x)+BatteryS10_293(33*Bat10Count+x)+BatteryS10_293(34*Bat10Count+x)+BatteryS10_293(35*Bat10Count+x)+BatteryS10_293(36*Bat10Count+x)+BatteryS10_293(37*Bat10Count+x)+BatteryS10_293(38*Bat10Count+x)+BatteryS10_293(39*Bat10Count+x)+BatteryS10_293(40*Bat10Count+x)+BatteryS10_293(41*Bat10Count+x)+BatteryS10_293(42*Bat10Count+x)+BatteryS10_293(43*Bat10Count+x)+BatteryS10_293(44*Bat10Count+x)+BatteryS10_293(45*Bat10Count+x)+BatteryS10_293(46*Bat10Count+x)+BatteryS10_293(47*Bat10Count+x)+BatteryS10_293(48*Bat10Count+x)+BatteryS10_293(49*Bat10Count+x)+BatteryS10_293(50*Bat10Count+x)+BatteryS10_293(51*Bat10Count+x)+BatteryS10_293(52*Bat10Count+x)+BatteryS10_293(53*Bat10Count+x)+BatteryS10_293(54*Bat10Count+x)+BatteryS10_293(55*Bat10Count+x)+BatteryS10_293(56*Bat10Count+x)+BatteryS10_293(57*Bat10Count+x)+BatteryS10_293(58*Bat10Count+x)+BatteryS10_293(59*Bat10Count+x)+BatteryS10_293(60*Bat10Count+x)+BatteryS10_293(61*Bat10Count+x)+BatteryS10_293(62*Bat10Count+x)+BatteryS10_293(63*Bat10Count+x)+BatteryS10_293(64*Bat10Count+x)+BatteryS10_293(65*Bat10Count+x)+BatteryS10_293(66*Bat10Count+x)+BatteryS10_293(67*Bat10Count+x)+BatteryS10_293(68*Bat10Count+x)+BatteryS10_293(69*Bat10Count+x)+BatteryS10_293(70*Bat10Count+x)+BatteryS10_293(71*Bat10Count+x)+BatteryS10_293(72*Bat10Count+x)+BatteryS10_293(73*Bat10Count+x)+BatteryS10_293(74*Bat10Count+x)+BatteryS10_293(75*Bat10Count+x)+BatteryS10_293(76*Bat10Count+x)+BatteryS10_293(77*Bat10Count+x)+BatteryS10_293(78*Bat10Count+x)+BatteryS10_293(79*Bat10Count+x)+BatteryS10_293(80*Bat10Count+x)+BatteryS10_293(81*Bat10Count+x)+BatteryS10_293(82*Bat10Count+x)+BatteryS10_293(83*Bat10Count+x)+BatteryS10_293(84*Bat10Count+x)+BatteryS10_293(85*Bat10Count+x)+BatteryS10_293(86*Bat10Count+x)+BatteryS10_293(87*Bat10Count+x)+BatteryS10_293(88*Bat10Count+x)+BatteryS10_293(89*Bat10Count+x)+BatteryS10_293(90*Bat10Count+x)+BatteryS10_293(91*Bat10Count+x)+BatteryS10_293(92*Bat10Count+x)+BatteryS10_293(93*Bat10Count+x)+BatteryS10_293(94*Bat10Count+x)+BatteryS10_293(95*Bat10Count+x)+BatteryS10_293(96*Bat10Count+x)+BatteryS10_293(97*Bat10Count+x)+BatteryS10_293(98*Bat10Count+x)+BatteryS10_293(99*Bat10Count+x))/100;
end
댓글 수: 0
답변 (1개)
sai charan sampara
2024년 5월 7일
편집: sai charan sampara
2024년 5월 7일
Hello,
You can pad some zeros at the end to make the array size divisible by 100 and then use the "reshape" command to divide into sets of 100 cycles. Then you can use the "mean" function to take the average over each cycle.
Here is an example code showing the same:
M=1:260 % Random array for testing
M=[M,zeros(1,40)] % Pad zeros at the end to make it a multiple of 100
M=reshape(M,100,[]); % Reshape into a matrix with 100 rows
M_avg=mean(M,1) % Take mean along each column
You can refer to these links for more information:
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!