[英]How can I replace a matrix element using logical indexing?
The following is my matrix, 以下是我的矩阵,
coords = [
1 -1;
1 0;
219 1;
219 2;
219 3;];
. 。
Suppose, I want to replace elements of the 2nd column , which are less than 1, with 1. 假设我想用1替换小于1的第二列的元素。
Ie, my expected matrix would be, 也就是说,我的预期矩阵是
coords = [
1 1;
1 1;
219 1;
219 2;
219 3;];
So, I tried to do the following, 因此,我尝试执行以下操作,
coords(:,coords(:, 2)<1) = 1;
It is not working. 它不起作用。
How to achieve that? 如何实现呢?
you almost got it: 您几乎明白了:
coords(coords(:,2)<1,2)=1;
it is the first entry you want to edit at the 2nd col... 这是您要在第二行编辑的第一个条目。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.