Get the segment number

19 Views Asked by At

let me try to explain what I want to do, with Wolfram Mathematica, with an example. I have subdivided a 1.7 meter long rod in several segments with offsets

{0., 0.533, 0.617, 0.819, 1.131, 1.455, 1.511, 1.7}

Let me give a position, e.g. x=0.56, I want to know in which segment that x is located. In this case x belong to segment=2.
If I give x=1.15 I should get segment=5.
If I give x=1.61 I should get segment=7.
And so on.
Obviously avoiding loops.
Many thanks for the help

1

There are 1 best solutions below

1
Teodoro Marinucci On

Found! Thanks anyway!

x := 1.61;
Count[MapThread[  GreaterEqual, {x - Offs, Table[0, Length[Offs]]}], True]

Fist of all I get a table containing only zeros:

Table[0, Length[Offs]]
{0, 0, 0, 0, 0, 0, 0, 0}

then I compare

x := 1.61; x - Offs

so I get an array containing positive numbers if x > Offs[nth element]

{1.61, 1.077, 0.993, 0.791, 0.479, 0.155, 0.099, -0.09}

The

MapThread[GreaterEqual, {x - Offs, Table[0, Length[Offs]]}]

allows to compare element by element the two arrays:

{True, True, True, True, True, True, True, False}

Finally I count the "True" elements.