Averaging sequential data if values are the same?

I want to be able to average the values of an array of n rows and 5 columns, based on the repeated values of a specific column.
I only want to average the values that are next to each other and not all that repeat. Such that:
%Original data:
key_column = [-6 -6 -6 -6 -5 -5 -3 -3 -3 -1 -1 -1 0 0 1 1 2 2 2 3 3 5 5 5 5 6 6 6 6 4 4 4 3 3 3 3 3 3 3 2 2 2 1 1 1 1 0 0 -1 -1 -2 -3 -5 -5 -5 -5 -5 -5 -5 -6 -6 -6];
%Ends up as:
key_column_avg = [-6 -5 -3 -1 -0 1 2 3 5 6 4 3 2 1 0 -1 -2 -3 -5 -6]
I'm running into trouble because the values of the repeated numbers appear at multiple points in the column and I only want the to be averaged if they are "next to each other" on the column. Also, I'm having issues because the number of repeated values isn't the same for each number. Sometimes the value repeats for 10 times, sometimes for 3, sometimes for 7, sometimes for 4.
Is it possible to do this?
Thank you.

4 commentaires

KL
KL le 10 Août 2017
Averaging means totally different. Do you just want to remove the repeating elements?
"...because the number of repeated values isn't the same for each number..."
Please attach small part your real data as mat - file.
yz
yz le 10 Août 2017
No, I want to average all columns based on the repeated sequential values of this specific column.
yz
yz le 10 Août 2017
Attached is a sample of my data set. Column 4 is the column of interest for the averaging, but I wish to apply the averaging to all columns (based on the n number of values on column 4)

Connectez-vous pour commenter.

 Réponse acceptée

key_column_avg = key_column(diff([0,key_column(:)'],1,2) ~= 0);

4 commentaires

Thank you!
Do you know how I can apply this averaging to all other columns that don't have repeated values?
What I mean:
key_column = [6 5 5 5 1 0 1 2 2 3 3 5 5 6]
other_column = [10 15 13 11 51 58 0 67 10 2 33 5 76 45]
%ends up being:
key_column_avg = [6 5 1 0 1 2 3 5 6]
other_column_avg = [10 13 51 58 0 38.5 17.5 40.5 45]
yz
yz le 10 Août 2017
Oops I think I replied on the wrong spot. Here it is again, just in case:
Attached is a sample of my data set. Column 4 is the column of interest for the averaging, but I wish to apply the averaging to all columns (based on the n number of values on column 4)
Thank you
Andrei Bobrov
Andrei Bobrov le 10 Août 2017
Modifié(e) : Andrei Bobrov le 10 Août 2017
d = data_example;
g = cumsum(diff([0;d(:,4)]) ~= 0);
out = splitapply(@mean,d,g);
or
d = data_example;
g = cumsum(diff([0;d(:,4)]) ~= 0);
[gg,c] = ndgrid(g,1:size(d,2));
out1 = accumarray([gg(:),c(:)],d(:),[],@mean);
yz
yz le 11 Août 2017
Thank you so much!

Connectez-vous pour commenter.

Plus de réponses (2)

KL
KL le 10 Août 2017
key_column_avg = key_column(diff([0 key_column])~=0);
José-Luis
José-Luis le 10 Août 2017
key_column = [-6 -6 -6 -6 -5 -5 -3 -3 -3 -1 -1 -1 0 0 1 1 2 2 2 3 3 5 5 5 5 6 6 6 6 4 4 4 3 3 3 3 3 3 3 2 2 2 1 1 1 1 0 0 -1 -1 -2 -3 -5 -5 -5 -5 -5 -5 -5 -6 -6 -6];
result = [key_column(diff(key_column) > 0), key_column(end) ]

Produits

Question posée :

yz
le 10 Août 2017

Commenté :

yz
le 11 Août 2017

Community Treasure Hunt

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

Start Hunting!

Translated by