以与Matlab中的向量相同的顺序查找向量的唯一值 [英] Find unique values of a vector with same order as in the vector in matlab

查看:161
本文介绍了以与Matlab中的向量相同的顺序查找向量的唯一值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个向量A = [2,5,6,2,4,13,34,3,34].我想找到此向量的唯一值,但未按排序顺序!我在Matlab网站上进行了搜索,发现了此功能

I have a vector A=[2,5,6,2,4,13,34,3,34]. I want to find a unique value of this vector but not in sorted order! I searched in Matlab site and I found this function

[C, ia, ic] = unique(A,'rows','stable')

,但是Matlab R2011a无法识别此功能!可能此功能适用于高于2011的版本!任何人都知道如何以与A like中相同的顺序找到A的唯一值: A = [2,5,6,4,13,34,3]

but this function is not recognized in Matlab R2011a ! probably this function works on version higher than 2011 !! anybody knows how can I find the unique values of A with the same order as in A like : A=[2,5,6,4,13,34,3]

推荐答案

假设您有 vector (因此'rows'版本没有意义),这是基于 bsxfun :

Assuming you have a vector (so the 'rows' version makes no sense), here's a solution based on bsxfun:

[~, ind] = max(bsxfun(@eq, A, A.'));
ind = ind(ind>=1:numel(ind));
C = A(ind);

工作原理:在元素(bsxfun(@eq, A, A.'))之间进行所有成对比较.对于每个元素,找到第一个相等元素([~, ind]=max(...))的索引.如果该索引小于当前位置(即,如果前一个元素等于当前位置),则忽略它(ind = ind(ind>=...).使用尚存的索引来生成结果(C = A(ind)).

How it works: Do all pairwise comparisons between elements (bsxfun(@eq, A, A.')). For each element, find the index of the first equal element ([~, ind]=max(...)). If that index is smaller than the current position (that is, if there's a previous element which is equal to the current one), disregard it (ind = ind(ind>=...). Use the surviving indices to generate the result (C = A(ind)).

这篇关于以与Matlab中的向量相同的顺序查找向量的唯一值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆