Need help in matrix dimension while converting from fortran to matlab
2 views (last 30 days)
Show older comments
Hello All, 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". 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.
2 Comments
Adam
on 20 May 2016
Do you not have a line number with that error? If not then use the debugger to work out exactly which line is causing the error at least.
I don't know Fortran, but judging by its 6 arguments I assume double_sum is 6D in the fortran code too so why is this suspicious?
Answers (2)
Elias Gule
on 20 May 2016
The error is due to the fact that at first double_sum is used as a variable to which a value is being assigned
double_sum = double_sum + mot(k,j,i,p)*str(k,j);
then later in equation
d(p)=x - x1(i,p) - x2(i,p)-double_sum(i,p,n,m,str,mot);
it is used as if double_sum is a function.
So this may be seen in two ways: 1) this equation may be trying to access an element in the array/matrix double_sum, whose position is is "i,p,n,m,str,mot", which doesn't seem to make sense. This may be the reason for the index exceeds matrix dimensions error. 2) this equation may be trying to call a function double_sum with the supplied parameters, if this is the case then this will fail because creation of the variable double_sum overrides the function name. So Matlab now sees double_sum as a variable rather than a function.
In the case of (2) above, I would suggest renaming the variable form of double_sum to something like "double_sum_", and leaving double_sum to only refer to the user-defined function.
Maybe this will help.
3 Comments
Elias Gule
on 24 May 2016
Ok, in this line
d(p)=x - x1(i,p) - x2(i,p)-double_sum(i,p,n,m,str,mot);
suggests that double_sum is used as function. In other programming languages, "b = a;" and "b = a();" respectively imply an assignment of some value to the variable "b", with the former assigning to "b" the value stored in a variable "a", and the latter assigning it the value returned by the function "a".
Your first usage of double_sum suggests that it is a variable, but the usage that I stated above appears to refer to a call to a function "double_sum". Please post the original Fortran code if you don't mind, I have done some Fortran to Matlab conversion before.
Walter Roberson
on 24 May 2016
In the original code, double_sum could be a 6-dimensional array before it is re-assigned as a scalar. However, my Fortran is rusty enough that I do not recall if that redefinition would be an error or if it would assign 0 to all elements of the 6 dimensional array or if there are circumstances involving dynamic memory allocation under which it might validly redefine to a scalar. (Considering the code is old enough to use numbered continuations, I doubt dynamic memory is a consideration.)
Guillaume
on 20 May 2016
I know nothing about fortran but it looks to me that your matlab code is not equivalent at all. I would have thought the translation would be:
for p = 1:m
d(p) = x - x1(i,p) - x2(i,p) - double_sum(i,p,n,m,str,mot)
end
%note that assuming that x1, x2, and double_sum are variables then the loop is equivalent to just
%d(1:m) = x - x1(i, 1:m) - x2(i, 1:m) - double_sum(i, 1:m, n, m, str, mot);
double_sum = 0;
for j = 1:m
for k = 1:n
if k ~= i
double_sum = double_sum + mot(k,j,i,p)*str(k,j)
end
end
end
0 Comments
See Also
Categories
Find more on Fortran with MATLAB 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!