help-octave
[Top][All Lists]

## Re: What happen when you substract two rows?

 From: Andreas Weber Subject: Re: What happen when you substract two rows? Date: Sun, 06 Jul 2014 21:18:15 +0200 User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Icedove/24.5.0

```Am 05.07.2014 20:16, schrieb burita:
> Hello,
> I'm a java JEE develloper and I'm having quite some trouble understanding
> this piece of code :
>
> c=0;
> for i=img
>    for j=1:rows(i)-1
>       c+=uint64(abs(i(j)-i(j+1)));
>    endfor
> endfor
> c
>
> img is a 3 dimensional array MxNx3Color, each color goes from 1 to 255
> what I don't understand is this : i(j)-i(j+1)

The inner loop tries to calculate the sum over the absolute valus
between the difference of subsequent elements.

The result of i(j)-i(j+1) is clamped to 0 because of uint8 arithmetic
and I don't know if this is intentional... See this example

octave:76> uint8(5)- uint8(7)
ans = 0

> It does image row n - imagne row n+1
Not, it doesn't, see above

If you have problemx understanding that code I suggest adding outputs
and create a small MxNx3 matrix for testing:

For example:

img = uint8(randi(255, 2, 3, 2))
c=0;
for i=img
i
for j=1:rows(i)-1
tmp = uint64(abs(i(j)-i(j+1)))
c+=tmp;
endfor
endfor
c

btw: if the output is really intentional, the hole code can be written as

c = sum(diff(flipdim(img, 1), 1, 1)(:))

which is *much* faster.

HTH, Andy

```