I have an array with either 1 or 0. From this, I am trying to create a new array with intervals.
I have some base interval, one step, we can say 1 second here. If there is 0, I skip, if there is 1 I create an interval based on a position, which means if 1 is in the first place of the input array, I'll create interval {1, 2} and then if the next one is also 1, I'll change the first interval to {1,3}.
I wrote this code that works, but I am trying to figure out a better way using functional methods like Fold/MapIndexed/Select to get the final array but it hurts my OOP brain
In:
fsetIntervalList[interval_, data_] := Module[{v = interval, d = data},
array = {};
For[i = 0, i < Length[d], i++,
If[d[[i + 1]] == 0, Continue[],
If[i == 0 || d[[i]] == 0, AppendTo[array, {i*v, i*v + v}],
array[[Length[array]]][[2]] += v]]
];
Return[array]
]
In:
fsetIntervalList[0.25, {1, 0, 0, 1, 1, 0}]
Out:
{{0., 0.25}, {0.75, 1.25}}
{ }
? $\endgroup${ }
, missed;
behindarray = {};
so there should be only one output, by callingarray
. Which is not the best obviously. $\endgroup$array
output. $\endgroup$