如何更改矩阵中多个点的值? [英] How can I change the values of multiple points in a matrix?
本文介绍了如何更改矩阵中多个点的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个[500x500]的矩阵。我有另一个[2x100]矩阵,它包含可能在第一个矩阵内的坐标对。我希望能够将第一个矩阵的所有值更改为零,而不需要循环。
I have a matrix that is [500x500]. I have another matrix that is [2x100] that contains coordinate pairs that could be inside the first matrix. I would like to be able to change all the values of the first matrix to zero, without a loop.
mtx = magic(500);
co_ords = [30,50,70; 30,50,70];
mtx(co_ords) = 0;
推荐答案
您可以使用函数 SUB2IND 将您的下标对转换为线性索引:
You can do this using the function SUB2IND to convert your pairs of subscripts into a linear index:
mtx(sub2ind(size(mtx),co_ords(1,:),co_ords(2,:))) = 0;
这篇关于如何更改矩阵中多个点的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文