Info

此问题已关闭。 请重新打开它进行编辑或回答。

Need help speeding up a loop

1 次查看(过去 30 天)
Darin McCoy
Darin McCoy 2015-3-11
关闭: MATLAB Answer Bot 2021-8-20
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 个)

此问题已关闭。

Community Treasure Hunt

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

Start Hunting!

Translated by