我需要将尺寸为3 * 8的二维矩阵(A)重塑为矩阵(B),其中维度为(6 * 4),如下例所示。
Matrix A
1 2 1 2 1 2 1 2
5 6 5 6 5 6 5 6
7 8 7 8 7 8 7 8
Matrix B
1 1 1 1
2 2 2 2
5 5 5 5
6 6 6 6
7 7 7 7
8 8 8 8
我尝试使用循环进行以下操作,但对于大型矩阵来说似乎非常慢。我想是否可以在Fortran 90中使用RESHAPE
函数进行重新整形。
counter=1
do i=1,size(A,2),2
seq1(counter)=i
counter=1+counter
end do
counter=1
do i=2,size(A,2),2
seq2(counter)=i
counter=1+counter
end do
counter=1
do i=1,size(A,1)*2,2
S_1(counter)=i
counter=1+counter
end do
counter=1
do i=2,size(A,1)*2,2
S_2(counter)=i
counter=1+counter
end do
Do i=1,size(A,1)
B(S_1(i),:)=A(i,seq1)
B(S_2(i),:)=A(i,seq2)
END DO
答案 0 :(得分:0)
以下代码进行了相应的重新整形,但您应该通过分析器运行程序来衡量性能。您的优化标志有多积极?
B(1:2,:) = reshape(source=A(1,:), shape=[2,4])
B(3:4,:) = reshape(source=A(2,:), shape=[2,4])
B(5:6,:) = reshape(source=A(3,:), shape=[2,4])
并扭转转型,
A(1,:) = reshape(source=B(1:2,:), shape=[8])
A(2,:) = reshape(source=B(3:4,:), shape=[8])
A(3,:) = reshape(source=B(5:6,:), shape=[8])