From: Haitham Tahbub on
Hi all ,

i have a function called the dynamic programming.

i have the m file function and i want to build this function in Simulink .
i tried days and weeks to bulid this as blocks but i failed .

I asked for this , they told me that converting it to s-function block is easier but i have no idea of s-function from m file .

help me please in imleminting the s-function code that i can load to the s-function block and use one block for this function ,

My function code is :-

function [p,q,D] = dp(M)
% [p,q] = dp(M)
% Use dynamic programming to find a min-cost path through matrix M.
% Return state sequence in p,q

[r,c] = size(M);

% costs
D = zeros(r+1, c+1);
D(1,:) = NaN;
D(:,1) = NaN;
D(1,1) = 0;
D(2:(r+1), 2:(c+1)) = M;

% traceback
phi = zeros(r,c);

for i = 1:r;
for j = 1:c;
[dmax, tb] = min([D(i, j), D(i, j+1), D(i+1, j)]);
D(i+1,j+1) = D(i+1,j+1)+dmax;
phi(i,j) = tb;
end
end

% Traceback from top left
i = r;
j = c;
p = i;
q = j;
while i > 1 & j > 1
tb = phi(i,j);
if (tb == 1)
i = i-1;
j = j-1;
elseif (tb == 2)
i = i-1;
elseif (tb == 3)
j = j-1;
else
error;
end
p = [i,p];
q = [j,q];
end

% Strip off the edges of the D matrix before returning
D = D(2:(r+1),2:(c+1));

==============
Thanks all
Regards
Haitham