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
Afficher commentaires plus anciens
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); ...
Réponse acceptée
Plus de réponses (1)
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 commentaires
Sulaymon Eshkabilov
le 28 Déc 2022
Saeid,
This given answer is completely correct, but you are not reading it correctly. All the best.
@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.
Catégories
En savoir plus sur Multidimensional Arrays dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!