1
   program oned_array
   implicit none
   integer i,j,k,seed,n,l
   parameter(n=10)
   real r0(n),s1(n)
   real m
   seed=4965817

!____________Generating the 1d array_________

   do i=1,n
         r0(i)=ran(seed)
         write(20,*)r0(i)     
   enddo
   k=1
   l=n/5
   do i=1,5
      m=0
      do j=k,l  
      s1(i)=r0(j)
      m=m+s1(i)
      write(21,*)i,j,s1(i)
      enddo

      k=l+1
      l=l+n/5

      write(22,*)i,m
   enddo

   stop
   end

I think this will work for a 1 dimensional array. But what to do for a matrix?

m is some quantity that I wanted to calculate for each subarray.

n is the older matrix dimension, ie 10.(10x10 matrix) M is something that I want to calculate This will convert the older matrix to 2*2 matrices and for each matrix I get m. Five 2*2 matrices.

ben10
  • 180
  • 4
  • 15
  • Please structure your code (indent). What do you mean by "convert"? Do you want 25 independent 4x4 matrices? Or one 25x4x4? Also convert under what rule(s)? Can you please explain your problem? IT will help with the responses you get if you present your problem thoughtfully and nicely. – zdim Mar 09 '16 at 09:20
  • @zdim i need 25 matrices(each 4x4) – ben10 Mar 09 '16 at 09:27
  • @zdim thank you.let me check.. – ben10 Mar 09 '16 at 09:41
  • @zdim but,it re-distribute the elements in a one-dimensional array into a multi-dimensional array.i need 25 independent matrices. – ben10 Mar 09 '16 at 09:52
  • 1
    What is the meaning of your code? Where are the variables declared? You should see `reshape()` and call 25 times in the right way. – Vladimir F Героям слава Mar 09 '16 at 10:12
  • (1) Array slices is something that you just must know with this language. It practically comes together with "array." (2) `reshape` is a great tool, once you get it you will get a lot of use of it. I suggest that you use this problem to learn these things. It is a good problem for that. Look them up, they are everywhere. Play a little and solve your problem. This is my suggestion. The harder part may turn out to be what to do with those matrices, how to organize that in the code (if you need to have all of them on hand at the same time). – zdim Mar 09 '16 at 11:08

1 Answers1

1

Extract submatrices using slices directly.

program sub_mat

! Better use 'allocatable' instead
integer, parameter :: nx = 20, ny = 20, sx = 4, sy = 4 
integer, dimension(nx, ny) :: mat 
integer, dimension(sx, sy) :: submat
integer :: i, j

! Initialize
mat = reshape( [ ((i+j, i = 1, nx), j = 1, ny) ], [nx, ny] )

! Access sx-by-sy blocks (submatrices)
do i = 1, nx, sx
    do j = 1, ny, sy
        submat = mat(i:i+sx, j:j+sy)
        ! Now you can do what is needed with 'submat'
    end do
end do

end program sub_mat

To print submatrices for review (right below submat = ... line above)

write(*,'(a,2(i2,a1))') 'at (', i, ',', j, ')'
    do m = 1, sx
        do n = 1, sy
            write(*, '(i4)', advance='no') submat(m,n)
        end do
        write(*, *)
end do

Ouput, edited for space

at ( 1, 1)
   2   3   4   5
   3   4   5   6
   4   5   6   7
   5   6   7   8
...
at ( 5, 9)
  14  15  16  17
  15  16  17  18
  16  17  18  19
  17  18  19  20
...
at (17,17)
  34  35  36  37
  35  36  37  38
  36  37  38  39
  37  38  39  40

The next learning step is to use reshape for the job instead. It is a higher-level tool with much power that is used for a variety of tasks. It is up for far more serious work, but this is a way to start with it.

Note that its simple use above, to initialize the matrix, is a way to solve the problem as well.
For a complete discussion of this example (to initialize the matrix), see this SO post.

zdim
  • 64,580
  • 5
  • 52
  • 81
  • @ben10 Most welcome. I hope my comments were not unsupportive. I did _not_ mean that -- on the contrary. – zdim Mar 10 '16 at 08:44