Info

La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.

Need help speeding up a loop

2 visualizaciones (últimos 30 días)
Darin McCoy
Darin McCoy el 11 de Mzo. de 2015
Cerrada: MATLAB Answer Bot el 20 de Ag. de 2021
Hi! So i'm trying to speed up a certain section of my code that is fairly slow... any ideas??? (Code is below.. the part that needs work is the for loop that says "NEEDS WORK")
%Initialization & sample data
timestep = 0.12; %Timestep in seconds
samp_number = 1000;
tempdata = [rand(samp_number,1)-0.3;rand(samp_number,1)-0.05]; %Starter data
data = repmat(tempdata,1000,1);
counter_channel = zeros(size(data)); %Init
myindex = find(data>0); %Index of all data that meets conditions
%%PART THAT NEEDS WORK... this will count up when an index is reached
%This for loop is slow, how can I do this faster?
for i = 1:size(myindex,1)
cur_data_index = myindex(i);
min_index = max(1,cur_data_index-1);
counter_channel(cur_data_index) = counter_channel(min_index) + timestep;
end
%%Plotting - just to show you what its doing
x = 1:(samp_number*2);
figure;
subplot(2,1,1);
plot(x,data(x))
title([sprintf('%d',2*samp_number) ' samples of random data']);
subplot(2,1,2);
plot(x,counter_channel(x));
title('Count of rand > -0.1');
ylabel('Seconds');

Respuestas (0)

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by