-1

I have this C# code and I am trying to convert it to MATLAB code.

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;
}

I don't think we can use float and int in MATLAB. How do I change the code?

HebeleHododo
  • 3,574
  • 1
  • 27
  • 37
user414981
  • 357
  • 1
  • 3
  • 9
  • 2
    Also, have a look at the Matlab documentation here: http://www.mathworks.com/access/helpdesk/help/techdoc/learn_matlab/bqr_2pl.html. It explains rather well how the program works. – Jonas Aug 09 '10 at 11:09
  • @ishamahajan: you seem to be using a different user, but if this related to your previous series of questions on neural networks, then the original code which is from here: http://stackoverflow.com/questions/1697243/help-with-perceptron/1714030#1714030 was actually in C\C++ not C# :) – Amro Aug 09 '10 at 11:11
  • Amro- U r very intelligent. But I am her rommie- Isha – user414981 Aug 09 '10 at 11:19
  • I and shilpa were trying to do 1 layer of neural network but as we are not supposed to do using neural toolbox, its getting difficult for us.Do you have the pseudo code for it?or I should directly use the eg. that you told us and change it completlty into matlab? would it work? – user414981 Aug 09 '10 at 11:23
  • 1
    if by 1-layer NN you mean a perceptron, then it should be straightforward to implement the *delta rule* for learning the weights (just like the C code I linked to). Otherwise if your goal is a full feed-forward multilayer perceptron, then its a bit more involved, and I suggest you refer to your textbook to understand the theory first before jumping to code.. – Amro Aug 09 '10 at 11:34
  • ya, I checked in the book and it is the same.A single layer neural network can represent Y = AX + b, and the weights in the network will represent A and b at the end.But Can I get the pseudo code for it so that I can easily implement it – user414981 Aug 09 '10 at 20:55
  • Amro ....can u see my other question that I posted today...I am doing so many mistakes that it is getting diificult to debug... – user414981 Aug 10 '10 at 04:37

2 Answers2

4

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
  • 121,265
  • 25
  • 232
  • 431
1

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
  • 9,203
  • 2
  • 34
  • 43