如何更改矩阵中多个点的值?

thr*_*ree 2 indexing matlab matrix variable-assignment

我有一个[500x500]的矩阵.我有另一个[2x100]矩阵,它包含可能在第一个矩阵内的坐标对.我希望能够将第一个矩阵的所有值更改为零,而不需要循环.

mtx = magic(500);
co_ords = [30,50,70;  30,50,70];
mtx(co_ords) = 0;
Run Code Online (Sandbox Code Playgroud)

gno*_*ice 7

您可以使用SUB2IND函数将您的下标对转换为线性索引:

mtx(sub2ind(size(mtx),co_ords(1,:),co_ords(2,:))) = 0;
Run Code Online (Sandbox Code Playgroud)