Your problem is with the first index (`i == 1`). You can't check if the previous element is < 0. You could adjust your range (the `1:length(outputarray)`) to only run through elements for which your if-condition makes sense.
-- Leah On Wed, Jul 30, 2014 at 5:12 PM, <[email protected]> wrote: > I'm a bit stuck on this one. Could I get one more hint about a way I could > get the same thing done without using the illegal indexing? > > On Wednesday, July 30, 2014 5:46:58 PM UTC-4, John Myles White wrote: > >> Yeah, it’s the combination of (a) the use of i and i+1 indexing with (b) >> the use of a loop that goes from i = 1 to i = length(outputarray). >> >> — John >> >> >> On Jul 30, 2014, at 2:44 PM, [email protected] wrote: >> >> To correct the bug, is it this? >> >> if outputarray[i] < 0 && outputarray[i+1] >= 0 >> count += 1 >> end >> >> Factoring in that Julia begins indexing from 1. >> >> On Wednesday, July 30, 2014 4:41:43 PM UTC-4, John Myles White wrote: >>> >>> This pseudocode almost works. Just replace Int64[1:len(outputarray)] >>> with 1:length(outputarray). >>> >>> There’s also a bug in your core logic, but I’ll leave fixing that as an >>> exercise to the reader. >>> >>> — John >>> >>> On Jul 30, 2014, at 1:03 PM, [email protected] wrote: >>> >>> Hi guys, >>> >>> I asked this in a previous thread, but because that diverged off-topic >>> from my existing question, I decided to create a new thread. >>> >>> Anyhow, say I have an array >>> >>> outputarray = Float64[-1.23423,-3.23423,-2.34234,-2.12342,1.23234,2. >>> 23423,-2.23432,5.2341,0.01111,1.23423] >>> >>> This array lists the output of some function. I want to count the number >>> of times that the function passes by or equals 0 while emerging from a >>> negative f(x). >>> >>> In pseudocode, I want to do: >>> >>> function counter(outputarray) >>> count = 0 >>> for i in Int64[1:len(outputarray)] >>> if outputarray[i] >= 0 && outputarray[i-1] < 0 >>> count += 1 >>> end >>> end >>> return count >>> end >>> >>> What would be the most efficient way of doing this in Julia? >>> >>> Thanks, >>> Wally >>> >>> >>> >>
