Skip to content
Prev 279272 / 398503 Next

Vectorization instead of loops problem

Costas: (and thanks for giving us your name)

which(x == 1)

gives you the indices where x is 1 (up to floating point equality --
you did not specify whether your x values are integers or calculated
as floating point, and that certainly makes a difference). You can
then use simple indexing to get the y values. No loops needed.

However, let's explore why your question may have been too poorly
formed to get the answer you seek:

1. What if the index of the first 1 is 3 or less? -- Do you want to
ignore the (less than 3) preceding values or just choose as many as
you can?

2. What if, as in your example, several 1's occur in x. Do you want
the 3 preceding values for all of them or just the first?

3. If the answer to 2 is "all of them," what if several 1's are less
than 3 indices apart -- do you want to include the overlapping sets of
3 y's -- or what?

My point is that "etc. etc." is simply inadequate as a coherent or
useful problem description in your post. You _must_ be explicit,
complete, and concise. This can be hard. Indeed, it may require
considerable thought and effort. I have found -- and others have often
noted here -- that going through such an exercise itself often reveals
a solution. But be that as it may, the Posting Guide is actually an
excellent, comprehensive discussion of how to ask good questions in
forums like this. Read it. Follow it.

... and to be fair, your post below is, imho, probably above average
as posts go, allowing me to focus on specific points that I thought
required clarification. Quite a few posts here of late have been so
muddled and incoherent that I had no clue what the OP wanted. And it's
not English as a second language. I am a language ignoramus and speak
only English, so I am happy to tolerate poor grammar and vocabulary
from someone for whom English is only one of several languages in
which they can communicate. The problem is poor thinking, not poor
English.

Best,
Bert
On Sun, Dec 4, 2011 at 7:18 AM, Costas Vorlow <costas.vorlow at gmail.com> wrote: