shannon_fano recursion error

1 view (last 30 days)
Maheen
Maheen on 17 Oct 2015
Answered: Geoff Hayes on 20 Oct 2015
function code1 = split(p)
add = 0;
s = zeros(size(p));
for i = 1:length(p)
add = add + p(i);
if(sum(p) - add) <= 0.5
s(i) = (p(i));
out2 = (s(s~=0));
code1 = zeros(size(out2))';
elseif (sum(p) - add) > 0.5
d(i) = (p(i));
out = (d(d~=0));
code2 = ones(size(out));
end
split(code1);
split(code2);
end
Getting the following error:
Maximum recursion limit of 500 reached. Use set(0,'RecursionLimit',N)
to change the limit. Be aware that exceeding your available stack space can
crash MATLAB and/or your computer.

Answers (1)

Geoff Hayes
Geoff Hayes on 20 Oct 2015
Maheen - executing recursive functions can be tricky if you don't code the appropriate stopping condition. Without such a condition, the function will be called again and again until you arrive at the error that you are observing.
Without knowing the input p that you are passing into this function, it is difficult to comment on your error, but I suspect that you don't want to call split when the length of p is one. This would be your stopping condition that you need to implement.
A couple of other things - within your for loop you have an if/elseif block. If the first condition is true then the local variable code1 is created, and if the second condition is true, then the local variable code2 is created. So only one (or none) can exist on the first iteration of the for loop. The problem is then with the two lines of code that follow
split(code1);
split(code2);
At least one of the above lines of code will throw an error (the Undefined function or variable message). You will only want to call the split if the variable exists for that iteration of the for loop.
Also, you are not capturing the return from either of these calls to split. You will need to so that the returned result can be concatenated (since you are implementing the Shannon-Fano algorithm) with the code prefix when creating the full code for the desired symbol.

Categories

Find more on MATLAB Mobile in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!