简体   繁体   English

如何使用逻辑索引替换矩阵元素?

[英]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.

相关问题 使用矩阵逻辑索引A(I)= B的Matlab错误 - Matlab error using matrix logical indexing A(I) = B 如何使用逻辑索引分隔矩阵中的单元格行? - How to separate rows of cells in a matrix using logical indexing? 如何使用逻辑索引和切片的组合在Matlab中“切分”矩阵? - How to “chop up” matrix in Matlab using combination of logical indexing and slicing? MATLAB:使用逻辑索引将矩阵中的值替换为其值除以2吗? - MATLAB: Use Logical indexing to replace the values in the matrix by its value divided by 2? 使用逻辑数组索引到矩阵 - Indexing into matrix with logical array 如何在3D矩阵中建立2D逻辑数组索引? - How do I do 2D logical array indexing in a 3D matrix? 如何使用逻辑索引MATLAB获得3d矩阵中每个切片中某些区域的平均值 - How to get mean values of certain regions in each slice in 3d matrix using logical indexing MATLAB Matlab:使用逻辑索引重复输入相同的向量到矩阵 - Matlab: enter same vector repeatedly to matrix using logical indexing 如何以坐标格式为稀疏矩阵执行单元格数组的逻辑索引? - How to perform logical indexing of cell array for sparse matrix in coordinate format? 如何在Matlab中对多维矩阵应用逻辑索引 - How to apply logical indexing on multi-dimension matrix in matlab
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM