How to average multiple vectors of different lengths?

I am taking multiple measurements (50~100) and want to take the mean of the measurements.
Here is how I am going about doing it: I define zero matrix of certain dimension to begin with, for example, zeros(1000,3,50) - each measurement is a matrix of 1000 rows and 3 columns. In this case, the 3rd dimension of 50 is to keep track of the 50 iterations of the measurement.
The issue is that, some measurement will end at row index 500 whereas some others at other row indices. If the longest measurement record the last value at row index 804, then I need to replicate all other measurements' last values up until row index 804 before taking appropriate average of all measurements.
Any ideas would be greatly appreciated!
Thanks,

4 Kommentare

If the longest measurement record the last value at row index 804, then I need to replicate all other measurements' last values up until row index 804 before taking appropriate average of all measurements.
That doesn't sound like it would be appropriate. Replicating values will add weight to the average that wasn't there before and alter the result, e.g.,
>> mean([1,2])
ans =
1.5000
>> mean([1,2,2,2,2,2,2,])
ans =
1.8571
Louis
Louis am 17 Jun. 2013
Yeah, I understand what you are saying. But the application depends on the situation, I believe. I am measuring the number of susceptible individuals at each time interval due to on-going epidemic. If one epidemic ends at time interval 3 with 10 susceptible individuals and another instance of epidemic ends at time interval 4 with 20 susceptible individuals, then I believe I need to average 10 and 20 susceptible individuals at time interval 4. (Assuming the number of susceptible individuals does not grow after the end of the epidemic)
Matt J
Matt J am 17 Jun. 2013
Bearbeitet: Matt J am 17 Jun. 2013
So you're averaging across the 3rd dimension, not the 1st? You say your input data is 1000x3x50. You therefore want a result that is 1000x3?
Louis
Louis am 17 Jun. 2013
That's right.

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Iain
Iain am 17 Jun. 2013

0 Stimmen

When you DON'T have a measurement, ensure that it is "NaN", when you come to take the average, use "nanmean", which ignores the NaN's in the calculations.
nanmean (or mean) can operate on a single dimension at a time...

2 Kommentare

Louis
Louis am 17 Jun. 2013
Thanks for the input. However, I don't this this would work. For example, if first measurement is [1;6;5;3;8;NaN] and second measurement is [2;4;7;NaN;NaN;NaN], I would like the last value of the second measurement to be copied to the indices 4 and 5, producing [2;4;7;7;7;NaN] so that they get averaged with the 4th and 5th indices of the first measurement. For the reason, please read over my comment to the previous answer by Matt J.
Iain
Iain am 18 Jun. 2013
Bearbeitet: Iain am 18 Jun. 2013
Ok.
Don't use nans, use zeros, take the first "slice", and take the "diff" of the rest of it
f = dataset(:,:,1);
d = diff(dataset,1,3);
get rid of the negatives (assuming your data is purely monotonic
d(d<0) = 0;
Put them together
f(:,:,2:(size(d,3)+1)) = d;
Take the cumulative sum:
f = cumsum(f,3);
means = mean(f,chosen_dim);

Melden Sie sich an, um zu kommentieren.

John
John am 18 Jun. 2013

0 Stimmen

Are any of your measurements exactly 0? Or, are any of them -1? Instead of initializing with zeros, you could initialize with M = -1*ones(1000,3,50). Then, you can do something like
for i = 1:50
[afterlastrow,c] = find(m(:,:,i)==-1);
lastrow = afterlastrow(1)-1;
m(m(:,1,i)==-1,1,i) = m(lastrow,1,i);
m(m(:,2,i)==-1,2,i) = m(lastrow,2,i);
m(m(:,3,i)==-1,3,i) = m(lastrow,3,i);
end

Kategorien

Mehr zu Audio I/O and Waveform Generation finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 17 Jun. 2013

Community Treasure Hunt

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

Start Hunting!

Translated by