How do I iterate over vector elements, but ignore elements which do not meet a condition?

2 views (last 30 days)
I have a piece of code, which I'm trying to make run faster. I've used the profiler and most of the time is spent on the following snippet:
for det = 1:N
if detect_vec(det)
result((det+1):(det+deadTime), m0, h) = zeros([1, deadTime]);
end
end
What this is doing is iterating over an array detect_vec which is binary data - each element is a 1 or a 0. If there is a 1, it then sets any other 1s following it to 0. deadTime is a variable which defines how many values after a 1 should be set to 0.
I'm sure there must be a way to make this more efficient; N is very large and so it makes sense that the profiler says the code spends most of its time here. I've tried using the find function by doing the following;
for det = find(detect_vec)
if detect_vec(det)
result((det+1):(det+deadTime), m0, h) = zeros([1, deadTime]);
end
end
This code runs without error but returns many more 1s than I'm expecting, and isn't really any quicker.
Any advice would be great.
  4 Comments
dpb
dpb on 11 Dec 2020
Unless the separation of elements in detect_vec is >deadTime you'll be resetting elements multiple times and moving the location of the string of zeros along with it.
Is this intended behavior? You could otherwise increment by the gap at least. Whether this reduces iterations or not would depend on the relative values and number of detection locations.
You may want to look at fillmissing with the 'MaxGap' option (if you have R2020b, I think is required).
Daniel Pollard
Daniel Pollard on 11 Dec 2020
The seperation of 1s in detect_vec is random, but on average larger than deadTime. The idea is to eliminate the possibility that detections made are not independent, so any that are too soon after another are set to zero.

Sign in to comment.

Accepted Answer

Matt J
Matt J on 11 Dec 2020
Edited: Matt J on 11 Dec 2020
z=zeros(1,deadTime);
for det = find(detect_vec);
result((det+1):(det+deadTime), m0, h) = z;
end
  1 Comment
Daniel Pollard
Daniel Pollard on 11 Dec 2020
Thank you so much for all your suggestions. I've tried all of them, and this one seems the most promising. I appreciate the effort you went to to help me.

Sign in to comment.

More Answers (2)

Matt J
Matt J on 11 Dec 2020
Edited: Matt J on 11 Dec 2020
e=zeros(1,deadTime);
I=conv( double(detect_vec~=0), [e+1,0,e] , 'same')>0;
result(I, m0, h)=0;
  2 Comments
Daniel Pollard
Daniel Pollard on 11 Dec 2020
This runs perfectly, and gives the same results as my previous code, but it runs takes twice as long to run as my code. Is there a way I can optimize the convolution? That's where the profilier says the code is bottlenecking.
Matt J
Matt J on 11 Dec 2020
Maybe use FFTs to do the convolution, or maybe,
e=ones(1,deadTime);
I=conv( double(detect_vec~=0), [e,0] , 'same')>0;
result(I, m0, h)=0;

Sign in to comment.


Matt J
Matt J on 11 Dec 2020
Edited: Matt J on 11 Dec 2020
Using interpMatrix from the File Exchange,
A=interpMatrix([0;ones(deadTime,1)],1,N,1); %compute only once and reuse!!!
result( A*detect_vec(:)>0 ,m,h)=0;

Categories

Find more on Operating on Diagonal Matrices in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!