I have been trying for a couple of days to implement a simple program from a Matlab code to Python. I am using numpy but if there is a better way I am open to it.
This is my Matlab code:
x=[1,1,1,9];
A=zeros(length(x),length(x));
for i=length(x):-1:1
for j=length(x):-1:i
if i==length(x)
A(i,j)=x(i);
else
if j>i
A(i,j)=A(i+1,j)+(j-i);
elseif j==i
A(i,j)=x(i)+min(A(j+1,j+1:end));
end
end
end
end
y=min(A(1,:));
It creates this matrix:
12 12 13 15
0 11 11 12
0 0 10 10
0 0 0 9
and the final result is 12 (the minimum from the first row). This is what I have done in Python so far:
import numpy as np
items = np.array([1,1,1,9])
sizeLimit = len(items)
P = np.zeros((len(items),len(items)))
for i in range(0, sizeLimit):
for j in range(i, sizeLimit):
if i == sizeLimit-1:
P[i,j] = items[i]
else:
if j > i:
P[i,j] = P[i+1,j] + [j-i]
elif j == i:
P[i,j] = items[i] + P[0, 0:].min()
print P
It seems it gets stuck somewhere and does not iterate.
I would be grateful to any help.