Locate a number in an array with ascending even and descending odd entries
Let's call the array x:

x = 2 4 6 8 1 9 7 5 3


I'm assuming you don't have the array stored in a variable (in which case this question boils down do: find(x==z)), and that you don't need the array either (in which case this question is really: "How do I create x?").



First off:

Don't use length as a variable name. length is a builtin function, and overloading it is a bad idea. Although it's better to use the builtin numel instead of length, overloading length is not a good idea. It might cause all sorts of strange bugs. Choose another variable name (I'll use len, you might use something else).



x has a fairly straight forward setup, and can be created quite simply using concatenation and the colon operator.

x = [2:2:len, 1, fliplr(3:2:len)]


If you can guarantee that len is odd then it's simply:

x = [2:2:len, 1, len:-2:3]


Now, you have x, and you know z. The only thing remaining is a simple call to find:

col = find(x==z);