1

I haven't made a code for it yet, however I will be making a Gauss-Seidel algorithm in Fortran, for solving very large matrices (actually to initiate the multi-gird method but that's irrelevant for this). As part of this, I wish to insert matrices into other matrices.

in MATLAB this is very simple, for example:

A=[1,2;3,4]
B=[A,A;A,A]

Outputs:

B =

1 2 1 2
3 4 3 4
1 2 1 2
3 4 3 4

as a 4x4 matrix.

However I have been having difficulties achieving the same with Fortran.

Please ignore the context - I don't require help with the GS/MG methods, I just need help embedding matrices (for other topics too), if at all possible!

Ian Bush
  • 6,996
  • 1
  • 21
  • 27
JP12321
  • 13
  • 2

1 Answers1

1

This can be done with array slices, e.g.

integer :: A(2,2)
integer :: B(4,4)

A(1,:) = [1, 2]
A(2,:) = [3, 4]

B(1:2,1:2) = A
B(3:4,1:2) = A
B(1:2,3:4) = A
B(3:4,3:4) = A

There are proposals to introduce better syntax for this in a future Fortran standard.

veryreverie
  • 2,871
  • 2
  • 13
  • 26