-2

I have been working on a fortran code to convert it into the matlab. I am facing some issues with dimensioning! Following is the code which is giving me error

   do 10 p = 1,m

    d(p) = 0.d0
    d(p) = x - x1(i,p) - x2(i,p) -
 &      double_sum(i,p,n,m,str,mot)

10      continue

double_sum = 0.d0

    do 10 j = 1,m
      do 20 k = 1,n
        if (k .eq. i) then
        else
          double_sum = double_sum + mot(k,j,i,p)*str(k,j)
        endif

20      continue
10    continue

to which I converted it into matlab as:

for p=1:m
                d(p)=0;
                double_sum = 0;
                for j=1:m
                    for k=1:n
                        if k==i
                        else
                          double_sum = double_sum + mot(k,j,i,p)*str(k,j);
                        end
                    end
                end
                d(p)=x - x1(i,p) - x2(i,p)-double_sum(i,p,n,m,str,mot);
            end

I am getting error of "index exceeding matrix".

The error line is for this part of my code:

d(p)=x - x1(i,p) - x2(i,p)-double_sum(i,p,n,m,str,mot);

So if I ignore double_sum(i,p,n,m,str,mot); this part, code runs perfectly.

I know the double_sum matrix is of 6D which looks suspicious to me, but I would like to have your support to successfully port this piece of fortran code.

Note:Asked the same question on matlab forum. But stackoverflow have more chances of people worked on fortran 77. Hence asking it here.

Adi kul
  • 63
  • 8
  • As you define it before in your code, `double_sum` is only a scalar. What are you trying to accomplish with your call to `double_sum(i,p,n,m,str,mot)`? – BillBokeey May 20 '16 at 11:29
  • After 1st initialization double_sum = double_sum + mot(k,j,i,p)*str(k,j) is also there. So from this I can say it's a 6D in fortran. Same I want to "translate" in matlab – Adi kul May 20 '16 at 11:36
  • Actually I am too struggling to understand the code. What I got is this code and I need to translate it with matlab. I have really low knowledge of fortran. Hence your reply matters a lot. – Adi kul May 20 '16 at 12:15
  • 3
    That Fortran lump is not valid. It contains (part of) one scoping unit, but there are two statements with the label `10`. Please look carefully at the original source. – francescalus May 20 '16 at 12:23
  • @roygvib agree, double_sum looks like a function doing a sum on the first two indexes. Therefore, the output is scalar (matrix element (i,p)). A similar function can be defined in matlab... – user1824346 May 20 '16 at 12:58
  • This is not fortran77. you can not do a scalar assignment to an array in f77, and obviously you can not assign to a function in any fortran version. – agentp May 20 '16 at 17:01

2 Answers2

1

If the Fortran code in the Question is really everything, it may be a very rough snippet that explains how to calculate array d(:)

do 10 p = 1, m
    d( p ) = x - x1( i, p ) - x2( i, p ) - double_sum( i, p, n, m, str, mot )
10 continue

with a function double_sum() defined by

double precision function double_sum( i, p, n, m, str, mot )
implicit none
integer,          intent(in) :: i, p, n, m
double precision, intent(in) :: str( n, m ), mot( n, m, ?, ? )
integer j, k

    double_sum = 0.d0

    do 10 j = 1, m
    do 20 k = 1, n
        if (k .eq. i) then
        else
            double_sum = double_sum + mot( k, j, i, p ) * str( k, j )
        endif
20  continue
10  continue
end

though it is definitely better to find the original Fortran source to check the context...(including how i and d(:) are used outside this code). Nevertheless, if we use the above interpretation, the corresponding Matlab code may look like this:

for p = 1:m

    double_sum = 0;
    for j = 1:m
    for k = 1:n
        if k == i
        else
            double_sum = double_sum + mot( k, j, i, p ) * str( k, j );
        end
    end
    end

    d( p ) = x - x1( i, p ) - x2( i, p ) - double_sum;    % <--- no indices for double_sum
end

There is also a possibility that double_sum() is a recursive function, but because we cannot use the function name as the result variable (e.g. this page), it may be OK to exclude that possibility (so the Fortran code has two scopes, as suggested by redundant labels 10).

Community
  • 1
  • 1
roygvib
  • 7,218
  • 2
  • 19
  • 36
0

There is an error in your loops. The fortran code runs one loop over p=1:m, whose end is marked by the continue statement. Then, two nested loops over j and k follow.

Assuming, you know the size of all your arrays beforehand and have initialized them to the correct size (which may not be the case given your error statement) this is more along the lines of the fortran example you posted.

 d = zeros(size(d));
 for p=1:m
     d(p)=x - x1(i,p) - x2(i,p)-double_sum(i,p,n,m,str,mot);
 end

% add a statement here  to set all entries of double sum to zero
double_sum = zeros(size(double_sum))

for j=1:m
    for k=1:n
        if k==i
        else
            double_sum = double_sum + mot(k,j,i,p)*str(k,j);
        end
    end
end

It is a little hard to give advice without knowledge of more parts of the code. are mot and str and double_sum functions? Arrays? The ambiguous choice of brackets in those two languages are hardly OPs fault, but make it necessary to provide further input.

Mohammed Li
  • 823
  • 2
  • 7
  • 22
  • Thank you for the reply. I tried the suggested code but now it gives "Undefined function or variable 'double_sum'." error on "double_sum = zeros(size(double_sum))" line. – Adi kul May 20 '16 at 12:12
  • Is double_sum a function or an array? If double_sum is a function, this means, you posted a recursive function, which would change quite a bit in the advice you would be receiving. – Mohammed Li May 20 '16 at 12:56