0

我很有趣将矩阵的对角线替换D1,2,3,4. 这是矩阵D

A=[1,2,3,4,2,3,4,5; 3,4,5,6,4,5,6,7];
D=[A;A];
D=[D D]; % size of matrix [4x16] %
4

1 回答 1

2

要将主对角线设置为从 1 开始并以 1 递增的整数:

D(eye(4)==1) = 1:4

或者概括一下:

n = min(size(D));
D(eye(n)==1) = 1:n;

请注意,这是将单位矩阵==1的输出转换为逻辑类型。eye(n)

编辑:

这只是对所有对角线的意思的猜测,但这里是:

n = size(D,1);
m = size(D,2);
I = repmat(eye(min([n,m])), ceil(n/m), ceil(m/n));
I = I(1:n, 1:m)==1
d = repmat(1:min([n,m]), 1, max([ceil(n/m), ceil(m/n)]));
d = d(1:max(m,n));
D(I) = d
于 2013-03-28T08:59:55.587 回答