help-octave
[Top][All Lists]
Advanced

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

Re: How to convert code m ade ​​in the octave to C?


From: Luke M
Subject: Re: How to convert code m ade ​​in the octave to C?
Date: Sun, 8 Apr 2012 19:14:57 -0700 (PDT)

> Thank you, but for example, how would this piece of code in C?
>
> (snip code with lots of for loops)

What do you want C code for?  I hope it's not just speed, because your code
is in such dire need of vectorization, I think you'd see around the same
speed up going from your loopy code to vectorized code as you would going to
native code.

For example this:
>for i = 1 : n
>    for j = 1 : m
>        w(i,j) = 0;
>    end
>end

Should be this:
w = zeros(n,m);

I'm not going to work it out myself but on cursory examination it looks
possible to get rid of all of your for loops with knowledge of matrix
multiplication, and logical indexing to get rid of some of your if
statements.

As an example of logical indexing, this:

>for k = 1 : m
>    if yl(1,k) >= limiar
>        target(1,k) = 1;
>    elseif yl(1,k) < limiar
>        target(1,k) = -1;
>    end
>end

Could be this:

target = ones(1,m);
target(yl < limiar) = -1;

But if you do really need C code, then you could just add more loops (you
have a bunch already!) where you do vector multiplication, or as Pascal
suggested, you could use the GNU Scientific Library (which uses the same
routines as Octave under the hood) for vector and matrix multiplication.

--
View this message in context: 
http://octave.1599824.n4.nabble.com/How-to-convert-code-made-in-the-octave-to-C-tp4539850p4542001.html
Sent from the Octave - General mailing list archive at Nabble.com.


reply via email to

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