Usually looping is not more expensive as performing some other operation which just hiddes the loop behind another function (e.g. Arrayfun). From your text it just sounds that you just chose the wrong algorithm. Your problem sounds very linear, that is O(n), but you write about loop in loop which means O(n^2).
With millions of elements quadratic runtime is not so nice.
Usually looping is not more expensive as performing some other operation which just hiddes the loop behind another function (e.g. Arrayfun). From your text it just sounds that you just chose the wrong algorithm. Your problem sounds very linear, that is O(n), but you write about loop in loop which means O(n^2).
With millions of elements quadratic runtime is not so nice. The algorithm you want is something like this: open = 0; for i=1:length(o) if (open == 0) open=o(i) else o(i) = 0; end if (c(i) ~= -open) c(i) = 0; else open = 0; end end It maybe needs some finetunig, as you didn't describe in detail e.g. What the order of the c and o signals are (e.g. If the same index opens and closes is first the open processed or the closed, my example code assumes open), or whether the order of the signals is always ok, or if there must be some error treatment - but I guess you get the idea of the single loop.
Thanks. Your suggestion is along the lines of what I am currently doing but since this piece of code accounts for the majority of my processing time, I was hoping that someone woule have a "magic" formula that would speed things up. :-) – Patrik Westerberg Mar 29 at 14:52 For the above code matlab needs less than one second on o and c vectors with 2.5 x 10^6 entries.
Basicly I see no way of accelerating it and esp I see no need for it - or do you some other calculations (or how many million entries do you have)? – flolo Mar 29 at 15:17.
You can use diff, along with some logical operations to get your answer. O=-1,-1,-1,0,0,0,1,1,0,0; oFinal=abs(diff(0,o)). *o; oFinal= -1 0 0 0 0 0 1 0 0 0 The trick is that the output of diff and your original vector o both have a non-zero value at the same index only for the first occurrence of the value in o (i.e.
, first occurrence in a chain). So, by multiplying it element-wise with o, you get your answer. The abs is to ensure that a sign change doesn't occur due to the output from diff.
The approach is similar for c, and I'll leave that for you to try :).
Thanks. I see that your solution does work for the example I gave. Unfortunately, I can have slighty more complex data, e.
G if o=1,1,0,1,0 and c=1,1,0,1,-1. In this case a diff of o would create o=1,0,0,1,0 (using your method with a leading zero) but I would actually want to ignore the signal in o(4)` since the o(1) signal is not closed until c(5). Many thanks for the suggestion though.
– Patrik Westerberg Mar 29 at 16:16 Sorry, I didn't understand your question earlier, but I get it now. It's a little more involved than simply using diff as I had done, but I'm fairly certain it can be done without loops by building on it. I don't have time right now, but I'll update it when I think of something.
– yoda Mar 29 at 20:03.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.