2

我有 n,n 维的矩阵 A。如何用列向量 x(大小为 n)替换 A 的每一列。

我想在没有任何“for/while”循环的情况下做到这一点,

有人可以帮帮我吗?谢谢。

4

2 回答 2

4

假设这是您的数据:

A = rand(11);
V = ones(size(A,1),1);

然后这是您将向量分配给矩阵的每一第二列的方式:

idx = 2:2:size(A,2)
A(:,idx) = repmat(V,numel(idx))
于 2013-10-25T10:13:19.113 回答
2
%// Create example data
n = 21
A = magic(n)
x = ones(size(A,1),1);
%// Replace every second column of A with x starting from the first column
m = ceil(size(A, 2)/2);
X = x(:, ones(1,m)); %//Replicate x
A(:,1:2:end) = X %// Put x in each odd column.

如果您希望它从第二列开始,那么您必须使用floor而不是ceil

%//Create example data
n = 6
A = magic(n)
x = ones(n,1);
%// Replace every second column of A with x starting from the second column
m = floor(size(A, 2)/2);
X = x(:, ones(1,m));
A(:,2:2:end) = X
于 2013-10-25T09:23:06.677 回答