Question

I am new with Mathematica and I have one more task to figure out, but I can't find the answer. I have two lists of numbers ("b","u"):

b = {8.734059001373602`, 8.330508824111284`, 5.620669156438947`, 
   1.4722145583571766`, 1.797504620275392`, 7.045821078656974`, 
   2.1437334927375247`, 2.295629405840401`, 9.749038328921163`, 
   5.9928406294151095`, 5.710839663259195`, 7.6983109942364365`, 
   1.02781847368645`, 4.909108426318685`, 2.5860897177525572`, 
   9.56334726886076`, 5.661774934433563`, 3.4927397824800384`, 
   0.4570000499566351`, 6.240122061193738`, 8.371962670138991`, 
   4.593105388706549`, 7.653068139076581`, 2.2715973346475877`, 
   7.6234743784167875`, 0.9177107503732636`, 3.182296027902268`, 
   6.196168580445633`, 0.1486794884986935`, 1.2920960388213274`, 
   7.478757220079665`, 9.610332785387424`, 0.05088141346751485`, 
   3.940557901075696`, 5.21881311050797`, 7.489624788199514`, 
   8.773397599406234`, 3.397275198258715`, 1.4847171141876618`, 
   0.06574278834161795`, 0.620801320529969`, 2.075457888143216`, 
   5.244608900551409`, 4.54384757203616`, 7.114276285060143`, 
   2.8878711430358344`, 5.70657733453041`, 8.759173986432632`, 
   1.9392596667256967`, 7.419234634325729`, 8.258205508179927`, 
   1.185315253730261`, 3.907753644335596`, 7.168561412289151`, 
   9.919881985898002`, 3.169835543867407`, 8.352858871046699`, 
   7.959492335118693`, 7.772764587074317`, 7.091413185764939`, 
   1.433673058797801`};

and

u={5.1929, 3.95756, 5.55276, 3.97068, 5.67986, 4.57951, 4.12308,
2.52284, 6.58678, 4.32735, 7.08465, 4.65308, 3.82025, 5.01325,
1.17007, 6.43412, 4.67273, 3.7701, 4.10398, 2.90585, 3.75596,
5.12365, 4.78612, 7.20375, 3.19926, 8.10662};

This is the LinePlot of "b" and "u";

plot

I need to compare first 5 numbers from "b" to 1st number in "u" and always leave the maximum (replace "b"<"u" with "u"). Then I need to shift by 2 numbers and compare 3rd, 4th, 5th, 6th and 7th "b" with 2nd "u" and so on (shift always => 2 steps). But the overlapping numbers need to be "remembered" and compared in the next step, so that always the maximum is picked (e.g. 3rd, 4th and 5th "b" has to be > than 1st and 2nd "u").

Possibly the easiest way would be to cover the maximums showed in the image throughout the whole function, but I am new to this software and I don't have the experience to do that. Still It would be awesome if someone would figure out how to do this with a function that would do what I have described above.

Was it helpful?

Solution

I believe this does what you want:

With[{n = Length @ u},
  Array[b[[#]] ~Max~ Take[u, ⌊{#-2, #+1}/2⌋ ~Clip~ {1, n}] &, 2 n + 3]
]
{8.73406, 8.33051, 5.62067, 5.1929, 5.55276, 7.04582, 5.55276, 5.55276, 9.74904,--

Or if the length of u and v are appropriately matched:

With[{n = Length @ u},
  MapIndexed[# ~Max~ Take[u, ⌊(#2[[1]] + {-2, 1})/2⌋ ~Clip~ {1, n}] &, b]
]

These are quite a lot faster than Mark's solution. With the following data:

u = RandomReal[{1, 1000}, 1500];
b = RandomReal[{1, 1000}, 3004];

Mark's code takes 2.8 seconds, while mine take 0.014 and 0.015 seconds.


Please ask your future questions on the dedicated Mathematica StackExchange site:

enter image description here

OTHER TIPS

I think that there's a small problem with your data, u doesn't have as many elements as Partition[b,5,2]. Leaving that to one side, the best I could do was:

Max /@ Transpose[
  Table[Map[If[# > 0, Max[#, u[[i]]], 0] &, 
    RotateRight[PadRight[Partition[b, 5, 2][[i]], Length[b]], 
     2 (i - 1)]], {i, 1, Length[u]}]]

which starts producing the same numbers as in your comment.

As ever, pick this apart from the innermost expression and work outwards.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top