MATLAB Answers


Creating a function with a logical output.

Asked by Craig
on 14 Feb 2013

Hi all,

I am quite confused. I have a function say like this:

function [output] = TLU( inputs, weights, threshold )
activationMagnitude = sum(weights.*inputs)
if activationMagnitude > threshold
output = true;
output = false;

But when I use class(ans) it always shows a char data type - despite me specifying the output as either true or false!

Why is this?

Best regards,


  1 Comment

Azzi Abdelmalek
on 14 Feb 2013

What ans represent?

2 Answers

Answer by Thorsten
on 14 Feb 2013
Edited by Thorsten
on 14 Feb 2013
 Accepted answer

There's nothing wrong with your function, you can just do it more concisely

function output = TLU( inputs, weights, threshold )
output = sum(weights.*inputs)  > threshold;


y = TLU([1 2 3], [ 4 5 6 ], 100)


 y =




ans =

But of course then class(ans) gives char (because ans is the string 'logical'), but this has nothing to do with the class returned by your function.


Answer by José-Luis
on 14 Feb 2013

It does return logical for me.

What does

 class(TLU(rand, rand, rand))

return? If ans returns something different, you may have overwritten its value before you call to class()

  1 Comment

Azzi Abdelmalek
on 14 Feb 2013

The question is what ans represent?

Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi test

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!