topic Hi Qun, in Intel® Fortran Compiler
https://community.intel.com/t5/Intel-Fortran-Compiler/How-to-accessing-a-2D-array-with-indexing-array/m-p/1157293#M141962
<P>Hi Qun,</P>
<P>It's a little hard to follow, but from my understanding you previously had declared dp(p), and had the k loop in matlab? And this worked? </P>
<P>My suggestion is to go back to basics, and rather than access as an array write as an explicit loop:</P>
<PRE class="brush:fortran;">integer*4 idx(n),ind(m),k,p,q
real*8 data(p,q),ipt(m),dp(p,q)
! New loop variable
integer*4 i
DO k=1,q
! dp(idx(ind),k) = 2*ipt*data(idx(ind),k);
! Replace with loop...
DO i = 1, m
dp(idx(ind(i)),k) = 2 * ipt(i) * data(idx(ind(i)),k)
END DO
END DO
</PRE>
<P> </P>
<P>Sorry if there's a mistake!</P>
<P><SPAN style="font-size: 1em;">I also think that your k loop should be going from 1 to q as the second dimension is sized q, not m - maybe this is the source of your trouble? </SPAN></P>
<P>FYI in Fortran [ ] or (/ /) are used to create an array - I don't think you need this for the example. </P>
<P>If this doesn't help try coding what you want in Matlab so it works, and post that here so that I, or others, can help.</P>
<P>Good luck,</P>
<P>Michael </P>Wed, 02 Aug 2017 14:51:01 GMTMichael_R_32017-08-02T14:51:01ZHow to accessing a 2D array with indexing array
https://community.intel.com/t5/Intel-Fortran-Compiler/How-to-accessing-a-2D-array-with-indexing-array/m-p/1157292#M141961
<P style="margin-bottom: 0px; color: rgb(0, 0, 0); font-family: "Microsoft YaHei", 微软雅黑; font-size: 14px;">Please help.</P>
<PRE class="brush:fortran;"> integer*4 idx(n),ind(m),k,p,q
real*8 data(p,q),ipt(m),dp(p,q)
DO k=1,m
dp(idx(ind),k) = 2*ipt*data(idx(ind),k);
ENDDO</PRE>
<P style="margin-bottom: 0px; color: rgb(0, 0, 0); font-family: "Microsoft YaHei", 微软雅黑; font-size: 14px;"> </P>
<P style="margin-bottom: 0px; color: rgb(0, 0, 0); font-family: "Microsoft YaHei", 微软雅黑; font-size: 14px;">My FORTRAN mex program has similar code as listed above. I need to recursively calculate specific elements of <STRONG>dp,</STRONG> column by column. The row indexes are given by the indexing arrays <STRONG>idx</STRONG> and <STRONG>ind</STRONG>. The results show that some of the elements are not properly set. I checked the program by setting break points in VS2013. It seems a indexing problem. My other version in which <STRONG>dp</STRONG> is 1D works very well. I also tried the following variations</P>
<P style="margin-bottom: 0px; color: rgb(0, 0, 0); font-family: "Microsoft YaHei", 微软雅黑; font-size: 14px;"> </P>
<PRE class="brush:fortran;"> dp([idx(ind)],k) = 2*ipt*data([idx(ind)],k); </PRE>
<P>Still without success. And the output seems somehow to be related to the optimization options /O2 or /O3.</P>
<P><SPAN style="color: rgb(0, 0, 0); font-family: "Microsoft YaHei", 微软雅黑; font-size: 14px;">I'm not familiar with Fortran. Only use it to write MEX to relieve the time-consuming part in MATLAB. </SPAN><STRONG style="font-size: 1em;">My question is when using indexing arrays to access specific elements of a 2D array, what's the meaning of the []? How can I properly code to realize the purpose.</STRONG></P>
<P><STRONG style="font-size: 1em;">Thanks.</STRONG></P>Sun, 30 Jul 2017 07:32:51 GMThttps://community.intel.com/t5/Intel-Fortran-Compiler/How-to-accessing-a-2D-array-with-indexing-array/m-p/1157292#M141961Qun_H_2017-07-30T07:32:51ZHi Qun,
https://community.intel.com/t5/Intel-Fortran-Compiler/How-to-accessing-a-2D-array-with-indexing-array/m-p/1157293#M141962
<P>Hi Qun,</P>
<P>It's a little hard to follow, but from my understanding you previously had declared dp(p), and had the k loop in matlab? And this worked? </P>
<P>My suggestion is to go back to basics, and rather than access as an array write as an explicit loop:</P>
<PRE class="brush:fortran;">integer*4 idx(n),ind(m),k,p,q
real*8 data(p,q),ipt(m),dp(p,q)
! New loop variable
integer*4 i
DO k=1,q
! dp(idx(ind),k) = 2*ipt*data(idx(ind),k);
! Replace with loop...
DO i = 1, m
dp(idx(ind(i)),k) = 2 * ipt(i) * data(idx(ind(i)),k)
END DO
END DO
</PRE>
<P> </P>
<P>Sorry if there's a mistake!</P>
<P><SPAN style="font-size: 1em;">I also think that your k loop should be going from 1 to q as the second dimension is sized q, not m - maybe this is the source of your trouble? </SPAN></P>
<P>FYI in Fortran [ ] or (/ /) are used to create an array - I don't think you need this for the example. </P>
<P>If this doesn't help try coding what you want in Matlab so it works, and post that here so that I, or others, can help.</P>
<P>Good luck,</P>
<P>Michael </P>Wed, 02 Aug 2017 14:51:01 GMThttps://community.intel.com/t5/Intel-Fortran-Compiler/How-to-accessing-a-2D-array-with-indexing-array/m-p/1157293#M141962Michael_R_32017-08-02T14:51:01Z