Select or add vectors to a matrix

I am trying to subtract matrix vectors. In other words, suppose I have a matrix A with elements

x1 x2 x3 x4 y1 y2 y3 y4 z1 z2 z3 z4 

I want to subtract vectors

 x1 y1 z1 

and

 x2 y2 z2 

How can I do this? I tried to do

 implict none real, dimension(3,4) :: A real,dimension(3) :: vector vector(1)=A(1,1)-A(1,2) vector(2)=A(2,1)-A(2,2) vector(3)=A(3,1)-A(3,2) 

However, this is rather rude. Also, this method would be impractical if I need to calculate a few sums or balances, especially when the matrix is ​​very large. I want to make it more elegant. Is there a way to specify a vector inside a matrix? Or is there a workaround to do this?

+5
source share
1 answer

You can specify slices of the array [start]:[end][:stride] (Fortran 2008 Standard, Cl. 6.5.3 "Elements of the array and sections of the array": R621). To select all elements according to a given size, select, for example, A(:,1) . Then your difference reads:

 implicit none real, dimension(3,4) :: A real,dimension(3) :: vector vector(:)=A(:,1)-A(:,2) 

or even

 vector=A(:,1)-A(:,2) 
+3
source

Source: https://habr.com/ru/post/1245810/


All Articles