How to implement a user defined recursive function which will update an array in its every call
조회 수: 2(최근 30일)
I am trying to implement Shanon Fano Encoding algorithm,
I have created a function shannon() as follows:
%to assign 0 to set1
%to assign 1 to set2
elseif (l1+1== len)
%recursive call on both the sets till the sets are no further divisible
Here, the shannon() function will create a Codeword matrix C(index,col) ,which will store the respective codeword of the symbols using Shannon Fano Coding.
I have defined another user defined function partition() ,which will divide the array of symbols into two nearly equal sum of arrays,
%P is the array (1 x N) storing probabilities of N symbols in descending order
%1 is the 1st codeword index which needs to be updated/stored in first call of shannon()
but the above program is not syntactically correct
so, can anyone tell me what should be the syntax of shannon() and what needs to be modified in the code so that it will return successfully the Codeword matrix?
I am attaching the main program and partition() function for convenience:
%Generation of N symbols
prompt='Enter number of symbols (N): ';
P= P/ (sum(P));
%Calculation of H
H= H+ (-1)*P(i)*log2(P(i));
%Implementation of the algorithm
function [arr_set1,arr_set2] =partition(arr)
%User Defined function to divide an array into two nearly equal sums of
if (sum2 < sum1 )
sum2=sum2 + arr(l);
set1(pos1) = arr(f);
sum1 = sum1 + arr(f);
Prudhvi Peddagoni 2021년 3월 25일
You can find the implementation of shaannon fano encoding here.
To update an array in the recursive function , you need to pass and return that array like this:
c = zeros(6,1);
a = 6;
c = factorial(c,a);
function c = factorial(c,a)
% c is an vector of size len(a) x 1. we need to compute factorials from 1
% to a and store them in vector c.
c(1) = 1;
c = factorial(c,a-1);
c(a) = a*c(a-1);
Hope this helps.