仅在由行索引向量指定的行中替换对角元素?(MATLAB)

Dar*_*rcy 0 indexing matlab matrix

我有一个N×N矩阵,A和一个行索引向量v.我想替换的对角线元素一个只为行中的一个被指定的v不使用for循环.

例如:

N = 10;
A = rand(N,N); %Random N x N matrix

v = [1 4 6 9 10]; %vector of row indices

%What I want to do but without a for loop:
for i = 1:length(v)
    A(v(i),v(i)) = 0;
end

%I thought this would work, but it does not:
%A(v,v) = 0;
Run Code Online (Sandbox Code Playgroud)

我觉得必须采用一种单行方法来做到这一点,但似乎无法弄清楚它会是什么.

干杯

Phi*_*ard 5

用途sub2ind:

A(sub2ind(size(A),v,v)) = 0;
Run Code Online (Sandbox Code Playgroud)