Average number of y for y=f(x) where there are several y values for the same x
1 view (last 30 days)
Show older comments
I have an array y where y is a function of x but for every x there are multiple values of y.
x=[x1 x1 x1 x1 x1 x2 x2 x2 x2 x3 x3 x3 x3 x3 ...]
y=[y1 y2 y3 y4 y5 y6 ... yn]
How can I separate the values of y related to each unique x and e.g. calculate their average (or maximum or minimum) to have an array of unique x and y elements at the end that would look like this:
x=[x1 x2 x3 ... xn]
y=[mean(y1:y5) mean(y6:y9) mean(y10:y14)...]
0 Comments
Accepted Answer
Voss
on 4 Feb 2022
% creating some x and y to replicate your situation, as I understand it:
x = repelem([1 2 3 4],1,5);
y = randn(size(x))+repelem(2:2:8,1,5);
disp(x); disp(y);
% Perform the requested task, as I understand it:
[ux,~,jj] = unique(x);
nux = numel(ux);
uy = zeros(1,nux);
for ii = 1:nux
uy(ii) = mean(y(jj == ii));
end
disp(ux); disp(uy);
0 Comments
More Answers (0)
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!