MATLAB: Multidimensional matrices from Fortran to Matlab

fortranmultidimensional

I am translating a code from Fortran to Matlab and have been stuck on how to deal with multidimensional arrays. Specifically,
Array's A dimensions are 20x20x400x5x4x65
In the Fortran code there is a division which I don't understand as follows:
sum(A(:,:,:,:,:,1))/sum(A(:,:,:,:,:,2)
In Matlab this would not work. Matlab would add each column of 2 dimensional matrices for every single page along the other dimensions (so A(:,:,1,1,1,1) would be one case).
I would greatly appreciate it if there is anyone familiar with Fortran to explain what the line above means and how it can be converted to Matlab. I read a lot of Fortran guides before coming here but I am really stuck on this one.

Best Answer

The sum function in Fortran sums all of the elements in the array. The / operation in Fortran is an element-wise divide. I.e., for arrays X and Y of the same size
Fortran Equivalent MATLAB
------- -----------------
sum(X) sum(X(:))
X / Y X ./ Y
X * Y X .* Y
The X(:) simply returns X reshaped into a column vector. So in your case, if you wanted it all on one line, the equivalent code would be:
Fortran Equivalent MATLAB
------- -----------------
sum(A(:,:,:,:,:,1))/sum(A(:,:,:,:,:,2)) sum(reshape(A(:,:,:,:,:,1),[],1))./sum(reshape(A(:,:,:,:,:,2),[],1))
Note: The result of the sums on the MATLAB code are scalars, so I could have used / instead of ./
Related Question