Azzera filtri
Azzera filtri

Rows mean collapse in big matrix

2 visualizzazioni (ultimi 30 giorni)
youngz
youngz il 26 Giu 2016
Risposto: Andrei Bobrov il 26 Giu 2016
Hi,
I have a big matrix (40448x47). I would like to collapse some rows with it mean. For example, if i have:
avarageNumb = 3
ma =
92 99 1 8 15 67 74 51 58 40
98 80 7 14 16 73 55 57 64 41
4 81 88 20 22 54 56 63 70 47
85 87 19 21 3 60 62 69 71 28
86 93 25 2 9 61 68 75 52 34
17 24 76 83 90 42 49 26 33 65
23 5 82 89 91 48 30 32 39 66
79 6 13 95 97 29 31 38 45 72
10 12 94 96 78 35 37 44 46 53
11 18 100 77 84 36 43 50 27 59
I would like 3 to have a matrix 4x10 with rows mean (first row is the mean of the first three, second row is the mean of the 4-5-6 rows, third row is the mean of 7-8-9 rows, and fourth row is the 10 row).
I'm trying to design an algo with nested loop, however i think that Matlab can help me with some function that i do not know (I am a newbie in Matlab)! :)
Thanks, Marco

Risposta accettata

Stephen23
Stephen23 il 26 Giu 2016
Modificato: Stephen23 il 26 Giu 2016
If the number of rows of M was an integer multiple of N then this would be easy to achieve by simply reshaping the matrix M. Because your data does not fit this we have to take into account that there may be from 1 to N last rows to be "collapsed" together. This can be achieved using mat2cell to split the matrix into groups, and then performing the mean inside a cellfun call:
% Number of rows to "collapse" together:
N = 3;
% Your data:
M = [...
92 99 1 8 15 67 74 51 58 40
98 80 7 14 16 73 55 57 64 41
4 81 88 20 22 54 56 63 70 47
85 87 19 21 3 60 62 69 71 28
86 93 25 2 9 61 68 75 52 34
17 24 76 83 90 42 49 26 33 65
23 5 82 89 91 48 30 32 39 66
79 6 13 95 97 29 31 38 45 72
10 12 94 96 78 35 37 44 46 53
11 18 100 77 84 36 43 50 27 5];
% vector of row counts:
row = N*ones(1,ceil(size(M,1)/N));
row(end) = mod(size(M,1)-1,N)+1;
% scalar of column count:
col = size(M,2);
% split data into cell, with the specified rows and columns in groups:
C = mat2cell(M,row,col);
% calculate mean of each group and concatenate back into one matrix:
out = cell2mat(cellfun(@(m)mean(m,1),C,'UniformOutput',false));
and the output in the command window:
>> out
out =
64.667 86.667 32 14 17.667 64.667 61.667 57 64 42.667
62.667 68 40 35.333 34 54.333 59.667 56.667 52 42.333
37.333 7.6667 63 93.333 88.667 37.333 32.667 38 43.333 63.667
11 18 100 77 84 36 43 50 27 5

Più risposte (1)

Andrei Bobrov
Andrei Bobrov il 26 Giu 2016
avarageNumb = 3;
ma =[...
92 99 1 8 15 67 74 51 58 40
98 80 7 14 16 73 55 57 64 41
4 81 88 20 22 54 56 63 70 47
85 87 19 21 3 60 62 69 71 28
86 93 25 2 9 61 68 75 52 34
17 24 76 83 90 42 49 26 33 65
23 5 82 89 91 48 30 32 39 66
79 6 13 95 97 29 31 38 45 72
10 12 94 96 78 35 37 44 46 53
11 18 100 77 84 36 43 50 27 59];
ii = ceil((1:size(ma,1))'/avarageNumb);
[jj,kk] = ndgrid(ii,1:size(ma,2));
out = accumarray([jj(:),kk(:)],ma(:),[],@mean);

Categorie

Scopri di più su MATLAB in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by