approximate derivative of array

Hey there, I have a profile of an edge region within an image that is in edge:(<1x29 uint32>). I have a second array of same type and length, that I want to store an approximate derivative in by subtracting each element with the following one (and disregarding the last element). The array produced does somehow only contain ones and twos and definately not what I am looking for. This might be easy, but I can't find it.
The loop I used was:
for i = 1:(xwidth)
lsf(i) = abs(edge((i+1)) - edge(i));
end
lsf(xwidth+1) = 0;
xwidth is the actual length minus one to not fall off the edge at the end. The iteration works so its not about the idizes.
Yurie

 Accepted Answer

Matt J
Matt J on 1 Mar 2013
Edited: Matt J on 1 Mar 2013
The DIFF command would be quicker than the for-loop, but the for loop should have worked and it's not clear (without seeing the contents of "edge") why lsf shouldn't contain ones and twos.

3 Comments

I tried the diff() and if I used it with manualy defined arrays it does exactly what I want. It does not however with the profile that I have stored in the array edge. It confuses me deeply because they were of the same data type.
for x = 1:((xwidth+1)) %filling histogram array
hhist(x) = (255 - I(ymin,(xmin+(x-1))));%retrieving the NEGATIVE (255-I(y,x))
end
edge = hhist; % It works until here. edge is fine.
hhist = diff(edge);
Can I upload .mat files somehow so that people can have a look into it? Here is the screenshot of my problem. The left plot is the edge, and the right should be the approx. derivative.
Thanks in advance!
Make sure that you convert edge to double first
hhist=diff(double(edge))
Otherwise, hhist will also be uint32 and won't be able to assume negative values. From your plots, though, the positive values of hhist look plausible, i.e., we can't tell from the plots that the positive transitions in edge are actually ever greater than 2.
In the middle of it there has to be one peak in the derivative. I checked and it was not there, otherwise yes the noise is not higher than +-2. I tried to convert to double first and that works fine. Thanks!

Sign in to comment.

More Answers (0)

Categories

Find more on Graph and Network Algorithms in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!