如何将向量b插入列col的矩阵中?我在Fortran中找不到任何语法以及插入或追加函数。
到目前为止,我所做的只是重新分配了列中的值,但我只想插入向量。
const concurrency = 1;
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min)) + min; //The maximum is exclusive and the minimum is inclusive
}
const queuePromises = (
[...(Array(20)).keys()]
.map(number => new Promise(
(resolve, reject) => setTimeout(() => resolve(number), getRandomInt(0, /* up to */ 250) /* milliseconds */)))
) ;
from(queuePromises).pipe(
mergeMap(qp => from(qp), concurrency)
)
.subscribe(
number => console.log(number, 'resolved')
, error => console.error('Individual Promise error', error),
() => console.log('all are resolved')
)
答案 0 :(得分:3)
如果我了解您的问题,您想:
n
的列数m
增加1; b
的内容作为新列插入m
中的索引col
处; m
的其余列,以免丢失任何数据。在这种情况下,您需要注意以下几点:
m
必须为allocatable
。因此,如果您要返回一个新的独立数组,则不需要这样做(但会创建一个额外的数据副本)。move_alloc
,从而避免了重新存储一个数组副本。这是一个演示实现:
program insert_vec
integer, allocatable :: m(:, :), b(:)
integer :: n = 3, col = 2, i
allocate(m(n, n))
allocate(b(n))
m = 10
b = [(i, i = 1, n)]
call insert(m, b, col)
do i = 1, n
print *, m(i, :)
end do
contains
subroutine insert(m, b, col)
integer, allocatable, intent(inout) :: m(:, :)
integer, intent(in) :: b(size(m, 1)), col
integer, allocatable :: temp(:, :)
integer :: rows, cols
rows = size(m, 1)
cols = size(m, 2)
allocate(temp(rows, cols + 1))
temp(:, 1:col) = m(:, 1:col)
temp(:, col) = b
temp(:, col + 1:cols + 1) = m(:, col:cols)
call move_alloc(temp, m)
end
end
我在gfortran 7.1.1上的输出是:
10 1 10 10
10 2 10 10
10 3 10 10