我怎样才能更通用?
XD_L
数组是手动介绍。
% Data
Xd_L = [10 100 1000];
C = zeros(length(Xd_L))
% Calc
C12 = -1/Xd_L(1);
C13 = -1/Xd_L(2);
C11 = abs(C12+C13);
C21 = C12;
C23 = -1/Xd_L(3);
C22 = abs(C21+C23);
C31 = C13;
C32 = C23;
C33 = abs(C31+C32);
% Result:
C = [C11 C12 C13; C21 C22 C23; C31 C32 C33]
答案 0 :(得分:1)
这是一种通用案例的方法 -
function out = scale_assign(Xd_L)
m = numel(Xd_L);
n = ceil(sqrt(2*m));
off_diag_vals = -1./Xd_L;
[y,x] = find(tril(true(n),-1));
out = zeros(n,n);
out(sub2ind(size(out),y,x)) = off_diag_vals;
out(sub2ind(size(out),x,y)) = off_diag_vals;
out(1:n+1:n^2) = abs(sum(out,2));
样品运行 -
>> Xd_L = [10 100 1000];
>> scale_assign(Xd_L)
ans =
0.11 -0.1 -0.01
-0.1 0.101 -0.001
-0.01 -0.001 0.011
>> Xd_L = [10 100 1000 10000 100000 1000000];
>> scale_assign(Xd_L)
ans =
0.111 -0.1 -0.01 -0.001
-0.1 0.10011 -0.0001 -1e-05
-0.01 -0.0001 0.010101 -1e-06
-0.001 -1e-05 -1e-06 0.001011