Average array values based on identical values.

I have data in array forms consisting of an x position(positive doubles), a specific y value associated with each x position and plane heights that the data falls on as z values. I want to average the y values for identical x values for each specific z value. For example:
A=[1.0 2.0 0.5; 1.0 4.0 0.5; 1.1 2.0 0.5; 1.1 4.0 0.6]
Result of new array is then [1.0 3.0 0.5; 1.1 2.0 0.5; 1.1 4.0 0.6]
So far I can average the y values for a specific x value with: newData = [unique( data(:,1) ),... accumarray( data(:,1), data(:,2), [], @mean )]
But I get stuck when including the z values.

 채택된 답변

Star Strider
Star Strider 2017년 10월 4일

1 개 추천

One approach:
A=[1.0 2.0 0.5; 1.0 4.0 0.5; 1.1 2.0 0.5; 1.1 4.0 0.6];
[Au,ia,ic] = unique(A(:,[1 3]), 'rows'); % Unique Rows For ‘A(:,[1 3])’ Only
y_mean = accumarray(ic, A(:,2), [], @mean); % Corresponding Mean Of ‘y’ Values
Result = [Au(:,1) y_mean Au(:,2)]; % Concatenate To Produce Desired Result Matrix
Result =
1 3 0.5
1.1 2 0.5
1.1 4 0.6

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

제품

질문:

2017년 10월 4일

답변:

2017년 10월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by