逐列填充具有矩阵元素的向量
Population of vector with matrix elements column by column
我的 IDE 是 CodeBlocks 16.01。
这是我的代码:
Program Matrix_To_Vector
Implicit none
Integer::i,j
Integer, parameter :: M = 3 , N = 2
Integer, dimension ( M , N ) :: Matrix_0
Integer, dimension ( M*N ) :: Vector_0
! Population of matrix
Do i = 1 , 3
Do j = 1 , 2
Matrix_0(i,j) = i+j
End Do
End Do
Open (15, File = 'Result.txt', Status = 'Unknown', Action = 'Write')
Do i = 1 , 3
Write(15,*) Matrix_0(i,:)
End Do
Write(15,*) ( Vector_0(i), i =1 , size(Vector_0))
Close (15)
End Program Matrix_To_Vector
矩阵填充结果为:
2 3
3 4
4 5
我的目的是用矩阵 Matrix_0
中的元素创建向量 Vector_0
。矢量的大小是M*N
。向量的第一个元素是矩阵中的 (1,1)
,最后一个元素是 (3,2)
- 我想逐列进行。
有没有办法用 do 循环做到这一点?
wanted vector 的内容是:
2 3 4 3 4 5
像这样?
do j=1,2
vector_0(3*(j-1)+1:3*(j-1)+3)=Matrix_0(:,j)
enddo
当然可以
vector_0=reshape(matrix_0,shape(vector_0))
还有
我的 IDE 是 CodeBlocks 16.01。 这是我的代码:
Program Matrix_To_Vector
Implicit none
Integer::i,j
Integer, parameter :: M = 3 , N = 2
Integer, dimension ( M , N ) :: Matrix_0
Integer, dimension ( M*N ) :: Vector_0
! Population of matrix
Do i = 1 , 3
Do j = 1 , 2
Matrix_0(i,j) = i+j
End Do
End Do
Open (15, File = 'Result.txt', Status = 'Unknown', Action = 'Write')
Do i = 1 , 3
Write(15,*) Matrix_0(i,:)
End Do
Write(15,*) ( Vector_0(i), i =1 , size(Vector_0))
Close (15)
End Program Matrix_To_Vector
矩阵填充结果为:
2 3
3 4
4 5
我的目的是用矩阵 Matrix_0
中的元素创建向量 Vector_0
。矢量的大小是M*N
。向量的第一个元素是矩阵中的 (1,1)
,最后一个元素是 (3,2)
- 我想逐列进行。
有没有办法用 do 循环做到这一点?
wanted vector 的内容是:
2 3 4 3 4 5
像这样?
do j=1,2
vector_0(3*(j-1)+1:3*(j-1)+3)=Matrix_0(:,j)
enddo
当然可以
vector_0=reshape(matrix_0,shape(vector_0))
还有