help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: 'for' loop vectorization


From: Alexander Barth
Subject: Re: 'for' loop vectorization
Date: Tue, 23 Oct 2007 15:06:35 -0400



On 10/23/07, Hermann Schwarting <address@hidden> wrote:
Hi,

I'd like to vectorize the following function that uses two for-loops.
I haven't found a way to express it with vectors but maybe you see
something I overlooked.


% D is a distance matrix between the points of two curves
[r, c] = size(D);

% cumulative maximum of the first column
for i = 2:r
  D(i, 1) = max(D(i-1, 1), D(i, 1));
end
% cumulative maximum of the first row
for j = 2:c
  D(1, j) = max(D(1, j-1), D(1, j));
end

% Propagation rule: The value of each matrix element is selected from
% it's starting value and it's left, upper, and upper-left neighbors.
for i = 2:r
  for j = 2:c
    D(i,j) = max(D(i,j), min( D(i,j-1), min(D(i-1,j-1), D(i-1,j)) ));
  end
end

% The result value is only the element D(r,c)


Typical sizes for D are 300x300 to 500x500. I use Octave 2.9.15.

Thanks,
Hermann


Hi Hermann,

What about this:
i = 2:r;
j = 2:c;
D(i, 1) = max(D(i-1, 1), D(i, 1));
D(1, j) = max(D(1, j-1), D(1, j));
D(i,j) = max(D(i,j), min( D(i,j-1), min(D(i-1,j-1), D(i-1,j)) ));
 
I have tested it for a random matrix.
Does this work for you?


Alex

reply via email to

[Prev in Thread] Current Thread [Next in Thread]