is it possible to change inner loops to make the script run faster?

hello
A small script I written takes a line and makes a cross correlation with all the lines after.
But it runs too slowly. could someone please advice?
Thanks.
clear Signal CROSS_FINAL MEAN_VAL stand_DIV
k=1;
num_run=1000;
start=440;final=1220; %indexes of the signal
for i=1:size(Untitled,2)
Signal(:,i)=Untitled(start:final,i)-mean(Untitled(start:final,i));
end
for i=1:num_run
i
for j=i+1:num_run
CROSS_FINAL(k,:)=xcorr(Signal(:,i),Signal(:,j));
k=k+1;
end
end
for i=1:final-start+1
MEAN_VAL(i)=mean(CROSS_FINAL(:,i));
stand_DIV(i)=std(CROSS_FINAL(:,i));
end

2 Commenti

How does Untitled look , Is it Matrix data?
Can you please post Untitled data?
It looks like the file attached but with 1000 columns

Accedi per commentare.

Risposte (1)

None of the loops are needed:
for i=1:size(Untitled,2)
Signal(:,i)=Untitled(start:final,i)-mean(Untitled(start:final,i));
end
is simply:
Signal = Untitled(start:final, :) - mean(Untitled(start:final, :), 1);
-
for i=1:num_run
i
for j=i+1:num_run
CROSS_FINAL(k,:)=xcorr(Signal(:,i),Signal(:,j));
k=k+1;
end
end
is exactly what xcorr does when passed a matrix (I'm assuming that num_run is equal to size(Signal, 2), so:
CROSS_FINAL = xcor(Signal);
-
for i=1:final-start+1
MEAN_VAL(i)=mean(CROSS_FINAL(:,i));
stand_DIV(i)=std(CROSS_FINAL(:,i));
end
is simply:
MEAN_VAL = mean(CROSS_FINAL, 1); %take mean across rows
stand_DIV = std(CROSS_FINAL, 1); %take standard deviation across rows
So, overall, the whole code can be simplified to:
start=440;final=1220; %indexes of the signal
Signal = Untitled(start:final, :) - mean(Untitled(start:final, :), 1); %take signal between start and final row and subract mean of each column
CROSS_FINAL = xcor(Signal); %cross correlation of each column against each column
MEAN_VAL = mean(CROSS_FINAL, 1); %take mean across rows
stand_DIV = std(CROSS_FINAL, 1); %take standard deviation across rows
Note that this also removes the need for any clear.

Tag

Richiesto:

il 27 Giu 2019

Risposto:

il 27 Giu 2019

Community Treasure Hunt

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

Start Hunting!

Translated by