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
12 views (last 30 days)
Show older comments
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); ...
0 Comments
Accepted Answer
Voss
on 27 Dec 2022
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)
% NewArray
xy = [XY(1,1:m:end); mean(reshape(XY(2,:),m,[]),1)]
2 Comments
More Answers (1)
Sulaymon Eshkabilov
on 27 Dec 2022
Eg. taking a matrix of 2-by-30:
A1 = [1:30; 4*(1:30)]
x = A1(1, 1:5:end)
y = mean(reshape(A1(2,1:end),6,5),2)
B1 = [x;y.']
3 Comments
Sulaymon Eshkabilov
on 28 Dec 2022
Saeid,
This given answer is completely correct, but you are not reading it correctly. All the best.
Voss
on 28 Dec 2022
@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)]
your_method = mean(reshape(A1(2,:),6,5),2).'
my_method = mean(reshape(A1(2,:),5,[]),1)
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))
mean(A1(2,2:6:end)) % etc.
See Also
Categories
Find more on Resizing and Reshaping Matrices 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!