1
$\begingroup$

I want to create a rule replacement list, which I then use to create a SparseArray. The input is a list of positions of the from {{startPosition, stopPosition}, ...}.

The following function works fine as long as the input has the startPosition and stopPosition in sequence.

buildBinaryTrace[pos_] := # -> 1 & /@ 
 Flatten[Subdivide[First@#, Last@#, Last@# - First@#] & /@ pos ] 

 pos = {{1,5}, {10,11}, {15,20}}

 buildBinaryTrace[pos]

Gives Result

{1 -> 1, 2 -> 1, 3 -> 1, 4 -> 1, 5 -> 1, 10 -> 1, 11 -> 1, 15 -> 1, 
  16 -> 1, 17 -> 1, 18 -> 1, 19 -> 1, 20 -> 1}

However the same function gives an Error due to the Subdivide function if the two positions are the same (indicating that only one number should be changed to a 1.

For example

tstPos = {{1,5}, {7,8}, {10,10}}

should give result

 { 1->1, 2-> 1, 3-> 1, 4-> 1, 5-> 1, 7-> 1, 8-> 1, 10-> 1}

but I get an error.

I have tried modifying the function to

 tstbuildBinaryTrace[pos_] := Module[{start = 0, stop = 0}, 
   {start, stop} = {First@#, Last@#} & /@ pos;
   If[stop - start != 0,
    # -> 1 & /@ Flatten[Subdivide[start, stop, stop - start] ],
    start -> 1]
   ] 

But some how i'm mapping incorrectly and get an error.

I'm looking for a solution that can either generate the correct list to create the sparse array or a solution that generates the correct SparseArray directly. I suspect making the SparseArray directly may be even faster/more elegant but I was unable to figure out how to do this. Speed is most important to me, so I will select the fastest solution if multiple solutions are provided.

$\endgroup$
5
  • $\begingroup$ Could tell more about how your desired sparse array should look like at the end? $\endgroup$
    – Kuba
    Dec 15, 2015 at 21:53
  • $\begingroup$ @kuba The sparse Array is representing a signal I am processing. The positions places where the signal is "on" get 1's in the sparse array and the positions where the signal is "off" get zeros. The positions i'm inputting into this function are the range of positions that the signal is "on". So the coordinates { {5,10}, {12,12} } indicates that the signal was off from frame 1-4. On from 5-10. Off at 11. and On at 12. The length of the sparse array should be the length of signal I am processing. In this example all my signals have length of 300. $\endgroup$
    – olliepower
    Dec 16, 2015 at 1:58
  • $\begingroup$ I see, thanks. Are there any concerns about the answer? $\endgroup$
    – Kuba
    Dec 16, 2015 at 11:29
  • $\begingroup$ @kuba no concern about answer. I am waiting to accept in case I get an answer that builds the sparse array directly so I can compare timings $\endgroup$
    – olliepower
    Dec 16, 2015 at 16:01
  • $\begingroup$ Sure, no need to hurry, just wanted to check if that fits your goal well or any adjustments are needed. Good luck. $\endgroup$
    – Kuba
    Dec 16, 2015 at 16:14

1 Answer 1

2
$\begingroup$

What about:

buildBinaryTrace := Thread[Flatten[Range @@@ #] -> 1] &

your problem will be fixed since Range[10,10] gives only {10}.


buildBinaryTrace@{{1, 5}, {7, 8}, {10, 10}}
{1 -> 1, 2 -> 1, 3 -> 1, 4 -> 1, 5 -> 1, 7 -> 1, 8 -> 1, 10 -> 1}
buildBinaryTrace@{{1, 5}, {10, 11}, {15, 20}}
{1 -> 1, 2 -> 1, 3 -> 1, 4 -> 1, 5 -> 1, 10 -> 1, 11 -> 1, 15 -> 1, 
 16 -> 1, 17 -> 1, 18 -> 1, 19 -> 1, 20 -> 1}
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.