From: Bob Hanlon on

Position is much more straightforward.

a = {{{0.08896779137, 0.08522648397},
{0.1162297255, 0.4316697935}},
{{0.6409512512, 0.3506400003},
{0.1156346501, 0.9537010025}},
{{0.8820963106, 0.9962655552},
{0.004333293427, 0.727745896}}};

Position[a, _?(0.3 < # < 0.7 &)]

{{1, 2, 2}, {2, 1, 1}, {2, 1, 2}}

Position[a, _?(# > 0.7 &)]

{{2, 2, 2}, {3, 1, 1}, {3, 1, 2}, {3, 2, 2}}


Bob Hanlon

---- Daniel Flatin <dflatin(a)rcn.com> wrote:

=============
I work in an environment where another system is the dominant analysis tool. In
porting some code to my preferred work environment, Mathematica, I find
that I occasionally need to reinvent functionality found in the other system. One
such function is find(). In that system, this function returns all the
non-zero indices in an array. Usually, this test array is the
consequence of a logical operation on each element, so that in the that system

indx == find(A > 3);

returns all the indices for which elements of A are greater than 3. I
have replicated this functionality in Mathematica, and I wanted to both
share it, and maybe get some input in how I could make it more
efficient or more elegant. One of the ways I learn to program in
Mathematica is to analyze all the various responses to simple questions
here, and I am hoping to steer the process here.

Here is my function:

findIndex[ array_?ArrayQ, test_ ] :== Module[
{n==Length[Dimensions[array]],idx},
idx == Cases[MapIndexed[If[test[#1],#2]&,array,{n}],{__Integer},n];
If[n====1,Flatten[idx],idx]
]

example:

(* set a *)

a ==
{{{0.08896779137,0.08522648397},{0.1162297255,0.4316697935}},{{0.6409512512,0.3506400003},{0.1156346501,0.9537010025}},{{0.8820963106,0.9962655552},{0.004333293427,0.727745896}}};

(*

get indices *)

indx == findIndex[a, 0.3 < # < 0.7&]

output:

{{1, 2, 2}, {2, 1, 1}, {2, 1, 2}}

and to verify this is a valid result:

Extract[a,indx]

returns

{0.4316697935,0.6409512512,0.3506400003}

as does

Select[Flatten[a], 0.3 < # < 0.7&]

Note that this function is quite a bit like Position[] except that it
works on results of a logical comparison rather than a pattern.
Position, on the other hand, has some a feature I view as a virtue. It
can operate on non-array objects, in fact, it can operate on non-list
objects.

If any readers has some insight into a more compact, elegant, or
Mathematica-like approach to this findIndex function, please feel free
to respond.

Anyway, thanks for your time, and in advance for your thoughts.
Dan



From: Daniel Flatin on
I want to thank everyone who responded to my question. Though I have
been using Mathematica for years, I am still learning. I didn't really
understand the use of conditionals in patterns before. Clearly,

Position[a, x_/;(0.3 < x < 0.7)]

is far more elegant and general than my solution. That is exactly what
I was looking for. Also, I have no idea how my = got doubled in my
post. I copied as text from working code.

Thanks again.
Dan

From: Simon on
Hi Dan,

Position does do what you want, you just need to restrict your
pattern:

In[1]:= a = {{{0.08896779137, 0.08522648397}, {0.1162297255,
0.4316697935}}, {{0.6409512512, 0.3506400003}, {0.1156346501,
0.9537010025}}, {{0.8820963106, 0.9962655552}, {0.004333293427,
0.727745896}}};

In[2]:= Position[a, _?(0.3 < # < 0.7 &)]
Out[2]= {{1, 2, 2}, {2, 1, 1}, {2, 1, 2}}

In[3]:= Extract[a, %] == Select[Flatten[a], 0.3 < # < 0.7 &]
Out[3]= True

Simon

On Mar 8, 7:15 pm, Daniel Flatin <dfla...(a)rcn.com> wrote:
> I work in an environment where another system is the dominant analysis to=
ol. In
> porting some code to my preferred work environment, Mathematica, I find
> that I occasionally need to reinvent functionality found in the other sys=
tem. One
> such function is find(). In that system, this function returns all the
> non-zero indices in an array. Usually, this test array is the
> consequence of a logical operation on each element, so that in the that s=
ystem
>
> indx == find(A > 3);
>
> returns all the indices for which elements of A are greater than 3. I
> have replicated this functionality in Mathematica, and I wanted to both
> share it, and maybe get some input in how I could make it more
> efficient or more elegant. One of the ways I learn to program in
> Mathematica is to analyze all the various responses to simple questions
> here, and I am hoping to steer the process here.
>
> Here is my function:
>
> findIndex[ array_?ArrayQ, test_ ] :== Module[
> {n==Length[Dimensions[array]],idx},
> idx == Cases[MapIndexed[If[test[#1],#2]&,array,{n}],{__Integer},n=
];
> If[n====1,Flatten[idx],idx]
> ]
>
> example:
>
> (* set a *)
>
> a ==
> {{{0.08896779137,0.08522648397},{0.1162297255,0.4316697935}},{{0.64095125=
12,0.3506400003},{0.1156346501,0.9537010025}},{{0.8820963106,0.9962655552},=
{0.004333293427,0.727745896}}};
>
> (*
>
> get indices *)
>
> indx == findIndex[a, 0.3 < # < 0.7&]
>
> output:
>
> {{1, 2, 2}, {2, 1, 1}, {2, 1, 2}}
>
> and to verify this is a valid result:
>
> Extract[a,indx]
>
> returns
>
> {0.4316697935,0.6409512512,0.3506400003}
>
> as does
>
> Select[Flatten[a], 0.3 < # < 0.7&]
>
> Note that this function is quite a bit like Position[] except that it
> works on results of a logical comparison rather than a pattern.
> Position, on the other hand, has some a feature I view as a virtue. It
> can operate on non-array objects, in fact, it can operate on non-list
> objects.
>
> If any readers has some insight into a more compact, elegant, or
> Mathematica-like approach to this findIndex function, please feel free
> to respond.
>
> Anyway, thanks for your time, and in advance for your thoughts.
> Dan


From: dh on
Hi Dan,
pattern can do more than you think. Therefore, you can use Position like
e.g.:
Position[a, x_ /; 0.3 < x < 0.7]
Daniel (Huber)

On 08.03.2010 12:15, Daniel Flatin wrote:
> I work in an environment where another system is the dominant analysis tool. In
> porting some code to my preferred work environment, Mathematica, I find
> that I occasionally need to reinvent functionality found in the other system. One
> such function is find(). In that system, this function returns all the
> non-zero indices in an array. Usually, this test array is the
> consequence of a logical operation on each element, so that in the that system
>
> indx == find(A> 3);
>
> returns all the indices for which elements of A are greater than 3. I
> have replicated this functionality in Mathematica, and I wanted to both
> share it, and maybe get some input in how I could make it more
> efficient or more elegant. One of the ways I learn to program in
> Mathematica is to analyze all the various responses to simple questions
> here, and I am hoping to steer the process here.
>
> Here is my function:
>
> findIndex[ array_?ArrayQ, test_ ] :== Module[
> {n==Length[Dimensions[array]],idx},
> idx == Cases[MapIndexed[If[test[#1],#2]&,array,{n}],{__Integer},n];
> If[n====1,Flatten[idx],idx]
> ]
>
> example:
>
> (* set a *)
>
> a ==
> {{{0.08896779137,0.08522648397},{0.1162297255,0.4316697935}},{{0.6409512512,0.3506400003},{0.1156346501,0.9537010025}},{{0.8820963106,0.9962655552},{0.004333293427,0.727745896}}};
>
> (*
>
> get indices *)
>
> indx == findIndex[a, 0.3< #< 0.7&]
>
> output:
>
> {{1, 2, 2}, {2, 1, 1}, {2, 1, 2}}
>
> and to verify this is a valid result:
>
> Extract[a,indx]
>
> returns
>
> {0.4316697935,0.6409512512,0.3506400003}
>
> as does
>
> Select[Flatten[a], 0.3< #< 0.7&]
>
> Note that this function is quite a bit like Position[] except that it
> works on results of a logical comparison rather than a pattern.
> Position, on the other hand, has some a feature I view as a virtue. It
> can operate on non-array objects, in fact, it can operate on non-list
> objects.
>
> If any readers has some insight into a more compact, elegant, or
> Mathematica-like approach to this findIndex function, please feel free
> to respond.
>
> Anyway, thanks for your time, and in advance for your thoughts.
> Dan
>


--

Daniel Huber
Metrohm Ltd.
Oberdorfstr. 68
CH-9100 Herisau
Tel. +41 71 353 8585, Fax +41 71 353 8907
E-Mail:<mailto:dh(a)metrohm.com>
Internet:<http://www.metrohm.com>


From: dh on


e.g.:

On 08.03.2010 12:15, Daniel Flatin wrote:

> Here is my function:

> findIndex[ array_?ArrayQ, test_ ] :== Module[

> example:

> output:

> and to verify this is a valid result:

E-Mail:<mailto:dh(a)metrohm.com>

Internet:<http://www.metrohm.com>



First  |  Prev  |  Next  |  Last
Pages: 1 2 3
Prev: Simple question
Next: corrected Lighting example in Help