How to code a function that calculates the redundancy of a given alphabet

4 views (last 30 days)
I have a function that calculates the entropy of vector element
function h = alph_entropy(P)
h = -sum(P .* log2(P));
endfunction
What should i do next? Thanks!
Redundancy of entropy function should use alph_entropy function
  2 Comments
KALYAN ACHARJYA
KALYAN ACHARJYA on 19 Nov 2020
Edited: KALYAN ACHARJYA on 19 Nov 2020
Save the function in diffenent MATLAB script as function file, later call the same in main script. Vary P and get different results of h, later plot(P,h);
You may learn Basics from MATLAB Onramp
Vadim Potorocha
Vadim Potorocha on 19 Nov 2020
So is it should be like this?
function r = redundancy_entropy(P)
r = alph_entoropy(P);
endfunction

Sign in to comment.

Accepted Answer

KSSV
KSSV on 19 Nov 2020
It looks like you are using Octave.
You can call functions in multiple ways.
Option 1:
function h = alph_entropy(P)
h = -sum(P .* log2(P));
end
Save the above function in a file and name it as alph_entropy.m. MATLAB takes the name default funciton name, but Octave you have to give the name. The name of the function and file name should be same. Then go to the folder where function is present and you can call this function in a file or in command window.
P = rand(100,1) ; % your P variable
h = alph_entropy(P) ; % calling function with P as input
Option 2: Annoymous function
h = @(P) -sum(P .* log2(P)) ; % this is a function handle
P = rand(100,1) ;
iwant = h(P)

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!