Average multiple vectors with different lengths

13 Ansichten (letzte 30 Tage)
Harm Gilsing
Harm Gilsing am 25 Mai 2020
Kommentiert: Rik am 25 Mai 2020
I performed 3 measurements with different lengths of time. I would like to average those measurements without losing data. The measurements are stored in separate excel files which I can load each iteration of a for loop.

Akzeptierte Antwort

Rik
Rik am 25 Mai 2020
Pad the shorter vectors with NaN and use the 'omitnan' flag in mean.
  2 Kommentare
Harm Gilsing
Harm Gilsing am 25 Mai 2020
Hi Rik, can you give a short example of this?
Rik
Rik am 25 Mai 2020
Sure:
A=[1 2 5];
B=[4 3];
C=[4 3 2 1 5];
alldata={A,B,C};%put them all in a cell for convenience
maxlen=max(cellfun('prodofsize',alldata));
for n=1:numel(alldata)
current_elem=numel(alldata{n});
if current_elem<maxlen
alldata{n}((current_elem+1):maxlen)=NaN;
end
end
%show the result of this loop
clc
[A,B,C]=deal(alldata{:})
mean([A;B;C],'omitnan')
%or better:
extradim=1+ndims(alldata{1});
bigarray=cat(extradim,alldata{:});
mean(bigarray,extradim,'omitnan')

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Data Import from MATLAB finden Sie in Help Center und File Exchange

Produkte


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by