逻辑索引而不是查找 [英] logical indexing instead of find
本文介绍了逻辑索引而不是查找的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想改善一段代码
LaserS_P=rand(20,3); % duumy data
LaserS_P(1:4:end,1)=0;
index_= find( LaserS_P(:,1)); % finds o element in first row of 20*3 matrix.
LaserS_P_new=LaserS_P(index_,:); % remove all rows (index_) from matrix
在这种情况下,如何使用逻辑索引.
How can i use logical indexing for this case.
推荐答案
要删除所有零值行(这不是您的示例所做的工作),可以按如下所示使用逻辑索引:
To remove all the zero-valued rows (which is not what your example does), you can use logical indexing as follows:
LaserS_P=rand(20,3); % dummy data
LaserS_P(1:4:end,1)=0;
index_= ( LaserS_P(:,1) == 0); %# finds 0 element in first col of 20*3 matrix.
%# index_ is a logical vector with ones wherever LaserS_P's first col is 0
LaserS_P_new=LaserS_P(~index_,:); %# remove all rows (index_) from matrix
%# alternatively
LaserS_P_new(index_,:) = []; %# remove all rows (index_) from matrix
这篇关于逻辑索引而不是查找的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文