Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Extreme optimization (can I vectorize this?)
Replies: 7   Last Post: Feb 26, 2013 12:50 PM

 Messages: [ Previous | Next ]
 Luca Posts: 77 Registered: 6/6/12
Extreme optimization (can I vectorize this?)
Posted: Feb 25, 2013 8:20 AM

Hi,
I came up with the following code to perform a calculation, that involves a "for" loop on a very large vector.
This takes as much as 9 minutes on a very old laptop. It's not a real problem actually, but I was wondering whether it is possible to vectorize this and improve the performances or if it's already at the best (or near it).
Or if anybody can tell me how to speed this up in any way, just for the sake of "optimal programming".
Here's my variables:
val : 1D vector of (single) "values". size (val) ~400k,1
tim: 1D vector of timestamps of the previous values, same size as before.
sign: 1D logical vector, of true or falses, computed with some algorithm that tells me if "something is happening". Same size as before

code:
clust = bwlabeln (sign);
maxima = zeros (max(sign(:)),1);
tMax = maxima;
for i=1:numel(maxima)
ind=(clust==i);
[maxima(i),I] = max(val(ind));
dummy = tim(ind);
tMax= dummy(I);
end

Here's some data:
numel (val(ind))~1-4
numel(maxima)~50k
the thing taking most of the "long" time is (clust==i) with ~9ms, and ~2ms for all of the rest.

Date Subject Author
2/25/13 Luca
2/25/13 dpb
2/25/13 Luca
2/25/13 dpb
2/26/13 Luca
2/26/13 Luca
2/26/13 dpb
2/26/13 Bruno Luong