MATLAB Answers

Need help speeding up a loop

1 view (last 30 days)
Darin McCoy
Darin McCoy on 11 Mar 2015
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');

  0 Comments

Sign in to comment.

Answers (0)

Sign in to answer this question.