Average multiple vectors with different lengths

조회 수: 15 (최근 30일)
Harm Gilsing
Harm Gilsing 2020년 5월 25일
댓글: Rik 2020년 5월 25일
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.

채택된 답변

Rik
Rik 2020년 5월 25일
Pad the shorter vectors with NaN and use the 'omitnan' flag in mean.
  댓글 수: 2
Harm Gilsing
Harm Gilsing 2020년 5월 25일
Hi Rik, can you give a short example of this?
Rik
Rik 2020년 5월 25일
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')

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Import from MATLAB에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by