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.