In an array xy=[x y], reduce array length by assigning average values of y based on a predefined range of x

조회 수: 3 (최근 30일)
I have the x values in the range x=[x1 ... xn] and y values related to x through the function f as in y=f(x). Now I want to break down down the y array to a smaller array Y in that I first find all values related to x in the range x1>x>x0 and then calculate the mean of y values whose corresponding x's are in this range. Then I move on to the next range which is x2>x>x1 and calculate the mean of the corresponding y values and so on. At the end, this will look like converting a xy larger dataset to a smaller one, as shown in the picture. Since this is usually a really large dataset, I'd rather do this without a loop.

답변 (1개)

Steven Lord
Steven Lord 2023년 5월 24일
Use findgroups or discretize to bin your X data into groups then use splitapply or groupsummary to calculate the @mean for each of those groups.
  댓글 수: 1
Dyuman Joshi
Dyuman Joshi 2023년 5월 24일
This is neat. Though I found it difficult to implement the other pair - findgroups with groupsummary.
x = [(1:20)' randi(50,20,1)];
disp(x)
1 46 2 7 3 40 4 41 5 34 6 8 7 38 8 34 9 46 10 21 11 8 12 3 13 47 14 46 15 4 16 31 17 11 18 42 19 21 20 21
%Defining range of values
idx = [1 4 8 12 16 20];
out = discretize(x(:,1),idx);
splitapply(@mean, x(:,2), out)
ans = 5×1
31.0000 30.2500 27.2500 25.0000 25.2000

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

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by