help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: building dynamic ranges from matrix


From: Juan Pablo Carbajal
Subject: Re: building dynamic ranges from matrix
Date: Wed, 22 Nov 2017 10:48:15 +0100

Hi,

If you want to save memory (wouldn't be my main concern, though) you
cannot vectorize this easily because the result of each row doesn't
have the same size. So arrayfun or cellfun might be your only option
(I feel there might be a solution using accumarray, haven't give a
thought to it, though).

If you do not care wasting memory for values that will be discarted,
then do the following (this is reasonable if you know that the gap
between the intervals is not huge)

M is your Nx2 matrix

x = min (M(:)):max (M(:));
tf = x >= M(:,1) & x <= M(:,2);
tf = or (tf(1,:),tf(2,:));
x(tf)

for the example given, the comparison with array fun follows

t_vec = t_fun = 0; for i=1:100;  M =
sort(round(100*(randn(10000,2))),2); tic; x = min(M(:)):max(M(:)); tf
= x >= M(:,1) & x <= M(:,2); tf = or(tf(1,:),tf(2,:)); x(tf); t_vec +=
toc; tic; x = arrayfun(@(x,y) x:y, M(:,1), M(:,2), "UniformOutput",
false);x=[x{:}]; t_fun += toc; endfor; printf ("Elapsed time is vec:%f
 fun:%f seconds.\n", t_vec/100, t_fun/100);

Elapsed time is vec:0.018486  fun:0.105738 seconds.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]