

This post was updated on .
Here is my problem.
Given a N X 2 matric, build a single vector that contains the range of
numbers between (:,1) and (:,2).
For example:
range_start_end = [1,5 ; 4 7];
should produce a vector containing [1:5,4:7], The final result being
[1,2,3,4,5,4,5,6,7]. I am trying to do this with just simple matrix
operation if possible no if's then's array functions etc.
I have tried this a few ways but can't seem to get it to work.. so I'd love
to hear any ideas.
Much appreciated!

Sent from: http://octave.1599824.n4.nabble.com/OctaveGeneralf1599825.html_______________________________________________
Helpoctave mailing list
Helpoctave@gnu.org
https://lists.gnu.org/mailman/listinfo/helpoctave


Example had an error it in. Should read
range_start_end = [1,5 ; 4 7]; should produce a vector containing [1:5,4:7], The final result being [1,2,3,4,5,4,5,6,7].
_______________________________________________
Helpoctave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/helpoctave


El dimarts, 21 de novembre de 2017, a les 14:06:38 EST, Anthony Gumbs va escriure:
Example had an error it in. Should read
range_start_end = [1,5 ; 4 7];
should produce a vector containing [1:5,*4:7*], The final result being
[1,2,3,4,5,4,5,6,7].
On Nov 21, 2017 12:47 PM, "AG" < [hidden email]> wrote:
Here is my problem.
Given a N X 2 matric, build a single vector that contains the range of
numbers between (:,1) and (:,2).
For example:
range_start_end = [1,5 ; 4 7];
should produce a vector containing [1:5,2:4], The final result being
[1,2,3,4,5,4,5,6,7]. I am trying to do this with just simple matrix
operation if possible no if's then's array functions etc.
I have tried this a few ways but can't seem to get it to work.. so I'd love
to hear any ideas.
Much appreciated!

Sent from: http://octave.1599824.n4.nabble.com/OctaveGeneralf1599825.html
_______________________________________________
Helpoctave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/helpoctave
You can try the following, it worked for me:
x = [1 5;4 7];
y = arrayfun(@(x,y) x:y, x(:,1), x(:,2), "UniformOutput", false);
z = [y{:}];
This code generalizes to any number of rows.
a.
_______________________________________________
Helpoctave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/helpoctave


El dimarts, 21 de novembre de 2017, a les 14:26:29 EST, avlas va escriure:
El dimarts, 21 de novembre de 2017, a les 14:06:38 EST, Anthony Gumbs va escriure:
Example had an error it in. Should read
range_start_end = [1,5 ; 4 7];
should produce a vector containing [1:5,*4:7*], The final result being
[1,2,3,4,5,4,5,6,7].
On Nov 21, 2017 12:47 PM, "AG" < [hidden email]> wrote:
Here is my problem.
Given a N X 2 matric, build a single vector that contains the range of
numbers between (:,1) and (:,2).
For example:
range_start_end = [1,5 ; 4 7];
should produce a vector containing [1:5,2:4], The final result being
[1,2,3,4,5,4,5,6,7]. I am trying to do this with just simple matrix
operation if possible no if's then's array functions etc.
I have tried this a few ways but can't seem to get it to work.. so I'd love
to hear any ideas.
Much appreciated!

Sent from: http://octave.1599824.n4.nabble.com/OctaveGeneralf1599825.html
_______________________________________________
Helpoctave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/helpoctave
You can try the following, it worked for me:
x = [1 5;4 7];
y = arrayfun(@(x,y) x:y, x(:,1), x(:,2), "UniformOutput", false);
z = [y{:}];
This code generalizes to any number of rows.
a.
The above message didn't seem to reach the mailing list :/
Trying again...
a.
_______________________________________________
Helpoctave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/helpoctave


El dimarts, 21 de novembre de 2017, a les 14:06:38 EST, Anthony Gumbs va escriure:
Example had an error it in. Should read
range_start_end = [1,5 ; 4 7];
should produce a vector containing [1:5,*4:7*], The final result being
[1,2,3,4,5,4,5,6,7].
On Nov 21, 2017 12:47 PM, "AG" < [hidden email]> wrote:
Here is my problem.
Given a N X 2 matric, build a single vector that contains the range of
numbers between (:,1) and (:,2).
For example:
range_start_end = [1,5 ; 4 7];
should produce a vector containing [1:5,2:4], The final result being
[1,2,3,4,5,4,5,6,7]. I am trying to do this with just simple matrix
operation if possible no if's then's array functions etc.
I have tried this a few ways but can't seem to get it to work.. so I'd love
to hear any ideas.
Much appreciated!

Sent from: http://octave.1599824.n4.nabble.com/OctaveGeneralf1599825.html
_______________________________________________
Helpoctave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/helpoctave
You can try the following, it worked for me:
x = [1 5;4 7];
y = arrayfun(@(x,y) x:y, x(:,1), x(:,2), "UniformOutput", false);
z = [y{:}];
This code generalizes to any number of rows.
_______________________________________________
Helpoctave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/helpoctave


Much appreciated. The only challenge with this is it uses an array fun 
which I was trying to avoid since I have a LOT of data and arrayfun seems to
be slower due to using loops in its implementation. What provided is good
because it generalizes to any number of rows. At runtime, the array of
range start end numbers is build and the number of rows is unknown so I
can't do something like:
rse = [1,5 ; 4 7];
a=[rse(1,1) : rse(1,2) , rse(2,1):rse(2,2)]
which was proposed by Doug.
Perhaps this can't be done without looping.

Sent from: http://octave.1599824.n4.nabble.com/OctaveGeneralf1599825.html_______________________________________________
Helpoctave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/helpoctave


Thanks Doug. The range start end matrix is built dynamically at runtime and
so the number of rows is unknown  could be 2 rows could be 10000.
Do you have any idea how your solution might be generalize for an unknown
number of rows at runtime?
I tried using cell arrays as an intermediary. I was trying to put each rows
ranges in a cell then joining all the ranges stored within the cell to the
final vector but I can't seem to get it to work. Appreciate the help.

Sent from: http://octave.1599824.n4.nabble.com/OctaveGeneralf1599825.html_______________________________________________
Helpoctave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/helpoctave


El dimarts, 21 de novembre de 2017, a les 18:46:57 EST, AG va escriure:
Much appreciated. The only challenge with this is it uses an array fun 
which I was trying to avoid since I have a LOT of data and arrayfun seems to
be slower due to using loops in its implementation. What provided is good
because it generalizes to any number of rows. At runtime, the array of
range start end numbers is build and the number of rows is unknown so I
can't do something like:
rse = [1,5 ; 4 7];
a=[rse(1,1) : rse(1,2) , rse(2,1):rse(2,2)]
which was proposed by Doug.
Perhaps this can't be done without looping.

Sent from: http://octave.1599824.n4.nabble.com/OctaveGeneralf1599825.html
_______________________________________________
Helpoctave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/helpoctave
Sounds good. I thought it was for an arbitrary N in:
> Given a N X 2 matric, build a single vector that contains the range of
> numbers between (:,1) and (:,2).
a.
_______________________________________________
Helpoctave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/helpoctave


El dimarts, 21 de novembre de 2017, a les 18:52:32 EST, AG va escriure:
Thanks Doug. The range start end matrix is built dynamically at runtime and
so the number of rows is unknown  could be 2 rows could be 10000.
Do you have any idea how your solution might be generalize for an unknown
number of rows at runtime?
I tried using cell arrays as an intermediary. I was trying to put each rows
ranges in a cell then joining all the ranges stored within the cell to the
final vector but I can't seem to get it to work. Appreciate the help.

Sent from: http://octave.1599824.n4.nabble.com/OctaveGeneralf1599825.html
_______________________________________________
Helpoctave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/helpoctave
Look arrayfun is very fast even for 10000 rows:
x = round(100*randn(10000,2));
tic,y = arrayfun(@(x,y) x:y, x(:,1), x(:,2), "UniformOutput", false);toc
Elapsed time is 0.0658202 seconds.
z=[y{:}];
size(z)
ans =
1 584615
a.
_______________________________________________
Helpoctave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/helpoctave


El dimarts, 21 de novembre de 2017, a les 19:01:18 EST, avlas va escriure:
El dimarts, 21 de novembre de 2017, a les 18:52:32 EST, AG va escriure:
Thanks Doug. The range start end matrix is built dynamically at runtime and
so the number of rows is unknown  could be 2 rows could be 10000.
Do you have any idea how your solution might be generalize for an unknown
number of rows at runtime?
I tried using cell arrays as an intermediary. I was trying to put each rows
ranges in a cell then joining all the ranges stored within the cell to the
final vector but I can't seem to get it to work. Appreciate the help.

Sent from: http://octave.1599824.n4.nabble.com/OctaveGeneralf1599825.html
_______________________________________________
Helpoctave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/helpoctave
Look arrayfun is very fast even for 10000 rows:
x = round(100*randn(10000,2));
tic,y = arrayfun(@(x,y) x:y, x(:,1), x(:,2), "UniformOutput", false);toc
Elapsed time is 0.0658202 seconds.
z=[y{:}];
size(z)
ans =
1 584615
a.
_______________________________________________
Helpoctave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/helpoctave
Forgot to add sort before to have always first index smaller (or equal) than second:
x = sort(round(100*(randn(10000,2))),2);
tic,y = arrayfun(@(x,y) x:y, x(:,1), x(:,2), "UniformOutput", false);toc
Elapsed time is 0.072608 seconds.
z=[y{:}];
size(z)
ans =
1 1126672
a.
_______________________________________________
Helpoctave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/helpoctave


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.
_______________________________________________
Helpoctave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/helpoctave


On Wed, Nov 22, 2017 at 10:48 AM, Juan Pablo Carbajal
< [hidden email]> wrote:
> 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)
Error: here should be
tf = or (mat2cell (tf,ones(N,1),size(tf,2)){:})
Also notice that this makes the union of all the intervals, so it
doesn't repeat indexes. It might not be what you are looking for, but
maybe you ca improve from here.
_______________________________________________
Helpoctave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/helpoctave


On Wed, Nov 22, 2017 at 10:57 AM, Juan Pablo Carbajal
< [hidden email]> wrote:
> On Wed, Nov 22, 2017 at 10:48 AM, Juan Pablo Carbajal
> < [hidden email]> wrote:
>> 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)
>
> Error: here should be
> tf = or (mat2cell (tf,ones(N,1),size(tf,2)){:})
>
> Also notice that this makes the union of all the intervals, so it
> doesn't repeat indexes. It might not be what you are looking for, but
> maybe you ca improve from here.
and with this correction the results seems to indicate arrayfun is the
way to go :D
Elapsed time is vec:0.207450 fun:0.103145 seconds.
_______________________________________________
Helpoctave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/helpoctave

