15,743,299 members
1.00/5 (1 vote)
See more:
I am working on a project to convert a large Fortran77 code into MATLAB. Since Do loops in F77 are equivalent to For loops in MATLAB, a subroutine of F77 when translated into MATLAB is giving error. The F77 subroutine is given in link[^]

and my tried MATLAB translated version is given in "What I have tried" section
which is giving following error

```Attempted to access G(0); index must be a positive integer or logical

HS(i)=FI^(NH)*G(2*i)/(G(NH-i)*G(i)*G(i-1));```

I  think I am unable to understand complex loop structure present in subroutine of Fortran77.

What I have tried:

G(1)=1.0;
NH=NS/2;
for i=2:NS
G(i)=G(i-1)*i;
end
HS(1)=2.0/G(NH-1);
for i = 2:NH
FI=i;
if i== NH
HS(i)=FI^(NH)*G(2*i)/(G(i)*G(i-1));
end
HS(i)=FI^(NH)*G(2*i)/(G(NH-i)*G(i)*G(i-1));
end
SN=2*(NH-NH/2*2)-1;
for i=1:NS
V(i)=0.0;
K1=(i+1)/2;
K2=i
if K2 > NH
K2 = NH;
end
for j=K1:K2
if 2*j-i == 0
V(i)=V(i)+HS(j)/(G(i-j));
elseif i == j
V(i)=V(i)+HS(j)/G(2*j-i);
end
end
V(i)=SN*V(i);
SN=-SN;
end
Posted
Updated 27-Apr-16 1:23am
Richard MacCutchan 24-Apr-16 9:06am
Looks like you initialise sets from 1 to N, but then try to access item 0 which does not exist.

## Solution 1

C#
```G(1)=1.0;
NH=NS/2;
for i=2:NS
G(i)=G(i-1)*i;
end
HS(1)=2.0/G(NH-1);
for i = 2:NH
FI=i;
if i== NH
HS(i)=FI^(NH)*G(2*i)/(G(i)*G(i-1));
else
HS(i)=FI^(NH)*G(2*i)/(G(NH-i)*G(i)*G(i-1));
end
end
SN=2*(NH-NH/2*2)-1;
for i=1:NS
V(i)=0.0;
K1=floor((i+1)/2);
K2=i;
if K2 > NH
K2 = NH;
end
for j=K1:K2
if 2*j-i == 0
V(i)=V(i)+HS(j)/(G(i-j));
elseif i == j
V(i)=V(i)+HS(j)/G(2*j-i);
else
V(i)=V(i)+HS(j)/(G(i-j)*G(2*j-i));
end
end
V(i)=SN*V(i);
SN=-SN;
end```