Matlab-用于将变量分类为组并输出组索引的命令 [英] Matlab - a command to sort variables into groups and output the group indices
本文介绍了Matlab-用于将变量分类为组并输出组索引的命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个向量,例如:a = [1 1 2 2 7 7 7 10 10 10 10 11 15].它可以是未排序的,但是在这里我将其写为已排序.我正在寻找将上述内容转换为[1 1 2 2 3 3 3 4 4 4 4 5 6]的Matlab命令.有什么好方法吗?
I have a vector, for example: a = [1 1 2 2 7 7 7 10 10 10 10 11 15]. It can be unsorted, but here I'm writing it as sorted. I am looking for a Matlab command that will convert the above to [1 1 2 2 3 3 3 4 4 4 4 5 6]. Is there a good way to do this?
推荐答案
至少有两种方法可以做到这一点
There's at least two ways to do this
(1)使用unique
的第三个输出:
(1) use the third output of unique
:
[~,~,out] = unique(a)
(2)使用统计工具箱中的grp2idx
(2) use grp2idx
from the statistics toolbox
out = grp2idx(a)
这篇关于Matlab-用于将变量分类为组并输出组索引的命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文