本文介绍了查找每个值的最后一次出现的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个像这样的向量:
I have a vector like this :
>> v = [1 1 1 2 2 3 4 4 4 4 4 5 5]'
v =
1
1
1
2
2
3
4
4
4
4
4
5
5
对向量进行排序.每个值可以有任意数量.我需要找到每个值最后一次出现的索引.在这种情况下,它将返回以下内容:
The vector is sorted. There can be any number of each values. I need to find the index of the last occurence of each value. In this case, it would return this :
answer =
3 % index of the last occurence of "1"
5 % index of the last occurence of "2"
6 % index of the last occurence of "3"
11 % index of the last occurence of "4"
13 % index of the last occurence of "5"
推荐答案
感谢@trumpetlicks,答案是 unique
.
Thanks to @trumpetlicks, the answer is unique
.
>> v = [1 1 1 2 2 3 4 4 4 4 4 5 5 6]'
v =
1
1
1
2
2
3
4
4
4
4
4
5
5
6
>> [~, answer] = unique(v)
answer =
3
5
6
11
13
14
在最新版本的MCR(R2013?)中, unique
的行为已更改.要获得相同的结果,必须使用 unique(v,'legacy');
In more recente version of the MCR (R2013 ?), the behavior of unique
has changed. To get the same result, you must use unique(v, 'legacy');
这篇关于查找每个值的最后一次出现的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!