在Matlab中找到所有类似的数组索引 [英] find all similar index of array in Matlab
问题描述
我有一个数组X=[1 2 3 1.01 2.01 4 5 1.01 3.01]
我想要这个数组的所有索引都相似并且在matlab答案中的区别<=0.01
是
I have a array X=[1 2 3 1.01 2.01 4 5 1.01 3.01]
I want to all index of this array are similar and difference<=0.01
in matlab answer is
X1=[1 4 8], X2=[2 5],X3=[3 9],X4=[6],X5=[7]
非常感谢
推荐答案
我认为提交内容具有宽容的独特性" 适合您.
I think the submission "unique with tolerance" in the FileExchange is for you.
您应该注意,将变量X1 ... X5创建为单独的变量是个不好的主意,也是一种不好的做法,因为这使得在以后的代码(矢量化或基于循环)麻烦且效率低下.存储数据的更正确替代方法是 cells
(例如(丹尼尔建议的解决方案)或
You should note, that creating the variables X1...X5 as separate variables is a bad idea and a bad practice, because this makes referencing these values in later code (which is either vectorized or loop-based) cumbersome and inefficient. More correct alternatives to storing the data are cells
(like in the solution suggested by Daniel) or in structs
.
已经说过,如果由于某种原因您仍然想要创建唯一命名的变量,则可以使用上述提交(uniquetol
)和eval
的组合来实现:
Having said that, if for some reason you still want to create uniquely named variables, this is possible using a mix of the aforementioned submission (uniquetol
) and eval
:
[~,b,c]=uniquetol(X,0.01+eps);
for ind1 = 1:length(b)
eval(sprintf('X%d = (find(c==X(b(%d))))'';',ind1,ind1));
end
这篇关于在Matlab中找到所有类似的数组索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!