tags:

views:

82

answers:

2
 float randomFloat()
    {
        return (float)rand() / (float)RAND_MAX;
    }

    int calculateOutput(float weights[], float x, float y)
    {
        float sum = x * weights[0] + y * weights[1] + weights[2];
        return (sum >= 0) ? 1 : -1;
    }

The above code is in C#. I need to convert this code into matlab code.I dont think, we can use float and int in matlab. How do I change the code?

+1  A: 

The built-in function rand() already does what you're trying to do with randomFloat().

For calculateOutput you can use something fairly similar to what you've got, but as you say you don't need to declare types:

function result = calculateOutput (weights, x, y)
s = x * weights(1) + y * weights(2) + weights(3);
if s >= 0
    result = 1;
else
    result = -1;
end
end

Note that matlab vectors are one-based, so you need to adjust the indexing.

If you want to generalise this to arbitrary vectors it would make sense to "vectorize" it, but for this simple case a straight translation like this should be fine.

walkytalky
+3  A: 

the first one is simply: rand()

the second function can be written as:

if ( [x y 1]*w(:) >=0 )
  result = 1;
else
  result = -1;
end
Amro

related questions