Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
Welcome to the Intel Community. If you get an answer you like, please mark it as an Accepted Solution to help others. Thank you!
26758 Discussions

How to accessing a 2D array with indexing array

Qun_H_
Beginner
140 Views

Please help.

  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

 

My FORTRAN mex program has similar code as listed above. I need to recursively calculate specific elements of dp, column by column. The row indexes are given by the indexing arrays idx and ind. 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 dp is 1D works very well. I also tried the following variations

 

   dp([idx(ind)],k) = 2*ipt*data([idx(ind)],k); 

Still without success.  And the output seems somehow to be related to the optimization options /O2 or /O3.

I'm not familiar with Fortran. Only use it to write MEX to relieve the time-consuming part in MATLAB. 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.

Thanks.

0 Kudos
1 Reply
Michael_R_3
Beginner
140 Views

Hi Qun,

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? 

My suggestion is to go back to basics, and rather than access as an array write as an explicit loop:

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

 

Sorry if there's a mistake!

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? 

FYI in Fortran [ ] or (/    /) are used to create an array - I don't think you need this for the example. 

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.

Good luck,

Michael 

Reply