Reduce a large XY array to a much smaller xy array where the x data is diluted to a much smaller vector and y values are the mean of the ones inbetween

조회 수: 6 (최근 30일)
I need to dilute a very large XY array where e.g. length(X)=10000 and I want to reduce the length of this array to a much smaller number. It is of course possible to reshape the array but this requires eleiminating all the elements inbetween. I need the y elements that make up the new y array to be an average of the elements that have been eliminated.
This means:
OldArray=XY=[X0 X1 X2 X3 X4 X5 X6 X7 X8 ... Xn; Y1 Y2 Y3 Y4 Y5 Y6 Y7 Y8 ... Yn];
NewArray=xy=[x1 x2 x3 ... xm; y1 y2 y3 ... ym]; m is a fraction of n
where
x1=X0; x2=X5; x3=X10; x4=15 ...
but
y1=mean(y0:y4); y2=mean(y5:y9); y3=mean(y10:y14); ...

채택된 답변

Voss
Voss 2022년 12월 27일
I'll assume that "Y1 Y2 ... Yn" should be "Y0 Y1 ... Yn" and that "y1=mean(y0:y4); y2=mean(y5:y9); y3=mean(y10:y14); ..." should be "y1=mean(Y0:Y4); y2=mean(Y5:Y9); y3=mean(Y10:Y14); ...".
n = 30;
m = 5;
% OldArray
XY = randi(10,2,n)
XY = 2×30
4 5 7 10 2 9 8 8 5 7 2 10 4 5 8 6 6 2 5 4 4 3 8 2 2 2 1 6 7 8 9 5 1 5 5 1 7 4 3 7 3 7 2 2 9 3 7 4 4 2 10 7 8 1 4 4 9 9 9 1
% NewArray
xy = [XY(1,1:m:end); mean(reshape(XY(2,:),m,[]),1)]
xy = 2×6
4.0000 9.0000 2.0000 6.0000 4.0000 2.0000 5.0000 4.4000 4.6000 4.0000 6.0000 6.4000

추가 답변 (1개)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2022년 12월 27일
Eg. taking a matrix of 2-by-30:
A1 = [1:30; 4*(1:30)]
A1 = 2×30
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 80 84 88 92 96 100 104 108 112 116 120
x = A1(1, 1:5:end)
x = 1×6
1 6 11 16 21 26
y = mean(reshape(A1(2,1:end),6,5),2)
y = 6×1
52 56 60 64 68 72
B1 = [x;y.']
B1 = 2×6
1 6 11 16 21 26 52 56 60 64 68 72
  댓글 수: 3
Sulaymon Eshkabilov
Sulaymon Eshkabilov 2022년 12월 28일
Saeid,
This given answer is completely correct, but you are not reading it correctly. All the best.
Voss
Voss 2022년 12월 28일
@Sulaymon Eshkabilov: I interpret "y1=mean(y0:y4);", etc., to mean that the 1st element of the 2nd row of the desired result should be the mean of the first 5 elements of the 2nd row of the input matrix, and so on similarly for the subsequent elements of the 2nd row of the result (mean of elements 6 through 10 of 2nd row of input matrix, then elements 11 through 15, and so on).
Using your example input matrix, the mean of the first 5 elements of the 2nd row (4, 8, 12, 16, 20) is 12, not 52, which is the mean of (4, 28, 52, 76, 100):
A1 = [1:30; 4*(1:30)]
A1 = 2×30
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 80 84 88 92 96 100 104 108 112 116 120
your_method = mean(reshape(A1(2,:),6,5),2).'
your_method = 1×6
52 56 60 64 68 72
my_method = mean(reshape(A1(2,:),5,[]),1)
my_method = 1×6
12 32 52 72 92 112
Do you interpret "y1=mean(y0:y4);" to mean that the 1st element of the 2nd row should be the mean of [y0 y6 y12 y18 y24], i.e., elements 1, 7, 13, 19, and 25 of the 2nd row of the input matrix?
mean(A1(2,1:6:end))
ans = 52
mean(A1(2,2:6:end)) % etc.
ans = 56

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

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by