我有一个300x178矩阵,我想找到该矩阵每列的最小值(1x178)。然后我希望,在每列中最小值的位置/像素处,从该列中所有其他像素处取和。我如何使用Fortran做到这一点?谢谢!
的例子:
1 4 6 3
2 6 7 4
5 1 5 7
就变成:
1 0 0 1
0 0 0 0
0 1 1 0
并最终:
8 0 0 14
0 0 0 0
0 11 18 0
正如@High Performance Mark所指出的,如果一个列有多个最小值,那么结果是模棱两可的——您是取第一个,将它们共享,还是…
?如果您取第一个最小值,则尝试以下操作。
module filter
implicit none
contains
function condenseColumns( A ) result( B )
integer, intent(in) :: A(:,:)
integer, allocatable :: B(:,:)
integer c
allocate( B, mold = A )
B = 0
do c = 1, size(A,2)
B( minloc( A(:,c), 1 ), c ) = sum( A(:,c) )
end do
end function condenseColumns
subroutine writeMatrix( A )
integer, intent(in) :: A(:,:)
integer r
do r = 1, size( A, 1 )
write( *, "( *( 1x, i3 ) )" ) A(r,:)
end do
end subroutine writematrix
end module filter
program matrix
use filter, only: condenseColumns, writeMatrix
implicit none
integer :: A(3,4) = transpose( reshape( [ 1, 4, 6, 3, 2, 6, 7, 4, 5, 1, 5, 7 ], [ 4, 3 ] ) )
integer, allocatable :: B(:,:)
B = condenseColumns( A )
call writeMatrix( A ); write( *, * ); call writeMatrix( B )
end program matrix
1 4 6 3
2 6 7 4
5 1 5 7
8 0 0 14
0 0 0 0
0 11 18 0