0

in matlab I am used to write something like this

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

So I extract the first column of matrix A and store it in matrix B, which is just a vector or a 2x1 Matrix. However I can't do this in Fortran, since it regards A(:,1) as an one dimensional array and thus gives me an error if I want to assign this to a "matrix" B of size 2x1. This is an minimal example in Fortran:

program test
    implicit none
    double complex, dimension(:,:), allocatable ::  A, B

    allocate(A(2,2), B(2,1))
    A = transpose(reshape((/ 1, 2, 3, 4/), shape(A)))
    B = A(:,1) !gives error that shape mismatch
end program test

Since I don't want to treat vectors separately in my algorithm, how can I achieve Matlab like behaviour?

v.tralala
  • 1,444
  • 3
  • 18
  • 39
  • Thank you all for the help. Unfortunately I couldn't find the old post. However I will upvote the anwser there too ;) – v.tralala Nov 14 '16 at 19:43

1 Answers1

3

Try

B = A(:, 1:1)

Or you should also be able to do this:

B(:,1) = A(:,1)

Either should work.

chw21
  • 7,970
  • 1
  • 16
  • 31