How to extract a new matrix from an existing one

I have a large number of records arranged in three columns. Sample data:

A=[1 3 2 3 5 4 1 5 ; 
   22 25 27 20 22 21 23 27; 
   17 15 15 17 12 19 11 18]'

I want the first column (clock) to control the entire matrix to create a new matrix as follows:

Anew=[1 2 3 4 5 ; 22.5 27 22.5 21 24.5; 14 15 16 19 15]'

If the second column Anewis the average of each corresponding hour, for example:

from the matrix A: at hour 1 we have 2 values ​​in the 2nd column, corresponding to hour 1 which are 22 and 23, so the average value is 22.5

Also the third column: at hour 1 we have 17 and 11, and the average value is 14, and this continues until hour 5. I use Matlab

+3
source share
2 answers

You can use ACCUMARRAY for this:

Anew = [unique(A(:,1)),...
        cell2mat(accumarray(A(:,1),1:size(A,1),[],@(x){mean(A(x,2:3),2)}))]

A(:,1) (x) 2 3 (mean(A(x,2:3),1)). cell2mat . ,

Anew = [unique(A(:,1)), ...
        accumarray(A(:,1),A(:,2),[],@mean), ...
        accumarray(A(:,1),A(:,3),[],@mean)]

.

, . . , Anew , . fillval accumarray NaN.

Anew = [(1:max(A(:,1)))', ...
        accumarray(A(:,1),A(:,2),[],@mean,NaN), ...
        accumarray(A(:,1),A(:,3),[],@mean,NaN)]
+4

, .

[Afinal(:,1),Afinal(:,2:3)] = consolidator(A(:,1),A(:,2:3),@mean);

Afinal
Afinal =
            1         22.5           14
            2           27           15
            3         22.5           16
            4           21           19
            5         24.5           15
+2

All Articles