0

First off, I do have to apologize, because I'm very sure that I'm simply making a simple mistake. I am making my first program in MatLab, and have been reading up on the relevant documentation, but simply still can't seem to solve my problem.

I am trying to implement the equation for information entropy in MatLab (I'm sure it probably already exists, but that's beside the point), but I am having issues with arrayfun as it seems to be calling entropySingle with no arguments.

I have the following functions in appropriately named files

function y = entropySingle(x)
y = x * log2(x);
end

and

function y = entropy(x)
if ~isvector(x)
    error('Input must be a vector');
end 
x = arrayfun(entropySingle, x);
y = sum(x);
end

and I'm calling entropy([1/3 1/4 1/6 1/8 1/12 1/24]). The error occurs on line 2 of entropySingle, but why is it being called with a null pointer? Thanks in advance,

0

1 Answer 1

1

You need to use element wise multiplication:

y = x .* log2(x);

Not using that small . before the multiplication tell matlab this is about matrix multiplication where it is not.

Also, don't use the name entropy. You are overwriting a built-in matlab function, and this just invites more trouble to your code and life in general.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I changed the code to name it something different and use .* instead, but I am still experiencing the same errors.
However, if I use an anonymous function, @(x) x .* log2(x), then everything works fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.