Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

Replacing a for loop?

조회 수: 3 (최근 30일)
jnaumann
jnaumann 2013년 11월 14일
마감: MATLAB Answer Bot 2021년 8월 20일
Hi,
I've written a small script to calculate the short time energy function of a signal. However it feels a bit clunky and my data sets are quite large so processing takes a while. Is there a way to speed up this process using vectorisation? This is the code at the moment.
function EImat=EI_mod(A,seg_length)
rms=sqrt(((sum(A.^2))/length(A)));
for i=1:(length(A))/seg_length;
A1=A((i*seg_length)-(seg_length-1):(i*seg_length));
rms1=sqrt(((sum(A1.^2))/seg_length));
EImat(i)=rms1;
end
Any help will be greatly appreciated.
Thanks
Jack
  댓글 수: 1
dpb
dpb 2013년 11월 14일
편집: dpb 2013년 11월 14일
nSeg=floor(length(A)/seg_length);
EImat=zeros(1,nSeg);
for i=1:nSeg
...
Gotta' run--but preallocating will surely at least help...

답변 (1개)

Roger Stafford
Roger Stafford 2013년 11월 14일
편집: Roger Stafford 2013년 11월 14일
EImat=sum(reshape(A,seg_length,[]).^2,1)/seg_length;
(Note: I assume here that the length of A is some integral multiple of seq_length.)
(Note 2: You don't return the 'rms' quantity in your function, so I haven't computed it here.)

태그

Community Treasure Hunt

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

Start Hunting!

Translated by