2

so I'm trying to know when an event happened before another event in matlab; by event I mean number. For example, I have a vector, let's say:

x = [0.3 0.3 0.1 0.2 0.5 0.1 0.3 0.1 0.5 0.1 0.4 0.5]

and I want to know in which position is the 0.1 that happened before a 0.5. I tried with find(x,0.5,'last') but that doesn't help much since I want to then find the 0.1. I thought about maybe creatig another vector that ended at the 0.5 and then search for the last 0.1 but that would just be sort of inefficient since my vectors contain ~300 events.

  • Can you show us the desired output in your example? Which of the positions with 0.1 are you trying to find? – beaker Dec 01 '16 at 17:56
  • The desired output would be Y =[3 8 10]; for example the 0.1 in position 6 wouldn't be reported because there is another .1 in position 8 before the 0.5 in position 9 – Fabiola Duarte Dec 01 '16 at 18:36

1 Answers1

2

You can try this if you want .5 immediately to appear after .1

idx = [x(1:end-1)==0.1 & x(2:end)== 0.5 false]

that generates a logical index, for numeric index you can use

find(idx)

Update: to find all .1 s that have .5 after them without having any .1 appear between those .1 and .5

f= find(x==.1 | x==.5)
f(x(f(1:end-1)) < x(f(2:end)))
rahnema1
  • 15,264
  • 3
  • 15
  • 27
  • Testing floating point values for equality is just asking for trouble. – beaker Dec 01 '16 at 17:52
  • 1
    Are you sure this is what the OP had in mind? This will only work if the 0.5 value is *immediately* after 0.1 – beaker Dec 01 '16 at 17:54
  • As beaker said, this only works if 0.1 is right before the 0.5 :/ and it will probably not work with other vectors. – Fabiola Duarte Dec 01 '16 at 18:35
  • I really can not surely say that it is what OP wants except in the comments or in edited question the expected output specified. I do not know... – rahnema1 Dec 01 '16 at 18:38
  • The comments section is there to ask for clarification. The answer section is there to give answers that work. – beaker Dec 01 '16 at 19:03
  • @beaker before your comment I was sure that the answer is what OP wants but your comment was useful and helped for clarification of the problem – rahnema1 Dec 01 '16 at 19:11