排序@排序和排名排列

发布于 2024-10-13 02:21:47 字数 1364 浏览 1 评论 0原文

正如 nazdrovje 所指出的(参见此处Ordering@Ordering 可用于获取列表中每个元素的排名。即使列表包含重复元素,结果也是一个 n 排列(视为整数 1 到 n 的有序列表,无重复),其中排名最低的元素分配为 1,第二低的元素分配为 2,正如 Andrzej Kozlowski 所指出的,以下内容成立(另请参阅此处):

(Sort@mylist)[[Ordering@Ordering@mylist]]==mylist

我愿意产生一个排名排列,其中最高排名元素被分配1,第二最高 2等,这样以下内容成立:

(Reverse@Sort@mylist)[[newPermutation]]==mylist

这看起来很简单,但我只是能够想出一个相当尴尬的解决方案。目前我正在执行以下操作:

newPermutation= Ordering@Ordering[Ordering@Ordering@mylist,All,Greater]

是否有更优雅或更直观的方法?肯定有吗?

示例:

mylist= {\[Pi],"abc",40,1, 300, 3.2,1};

Ordering@Ordering@mylist

Ordering@Ordering[Ordering@Ordering@mylist,All,Greater]

输出(注意排列之间的倒数关系)

{7,6,4,1,5,3,2}
{1,2,4,7,3,5,6}

(以下两者均计算为 True)

Sort@mylist)[[Ordering@Ordering@mylist]]== mylist
Reverse@Sort@mylist)[[ Ordering@Ordering[Ordering@Ordering@mylist,All,Greater]]]== mylist

As pointed out by nazdrovje (see here) Ordering@Ordering may be used to obtain the rank of each element in a list. Even when the list contains repeated elements the result is an n-permutation (taken as an ordered list of integers 1 to n without repetition), where the lowest ranked element is assigned 1, the second lowest 2, etc. As pointed out by Andrzej Kozlowski, the following holds (see also here):

(Sort@mylist)[[Ordering@Ordering@mylist]]==mylist

I'd like to produce a ranking permutation where the highest ranked element is assigned 1, the second highest 2, etc. such that the following holds:

(Reverse@Sort@mylist)[[newPermutation]]==mylist

This seems simple, but I have only been able to come up with quite an awkward solution. At the moment I do the following:

newPermutation= Ordering@Ordering[Ordering@Ordering@mylist,All,Greater]

Is there a more elegant,or more intuitive, way? There surely must be?

An example:

mylist= {\[Pi],"abc",40,1, 300, 3.2,1};

Ordering@Ordering@mylist

Ordering@Ordering[Ordering@Ordering@mylist,All,Greater]

Output (note the reciprocal relationship between the permutations)

{7,6,4,1,5,3,2}
{1,2,4,7,3,5,6}

(Both the following evaluate to True)

Sort@mylist)[[Ordering@Ordering@mylist]]== mylist
Reverse@Sort@mylist)[[ Ordering@Ordering[Ordering@Ordering@mylist,All,Greater]]]== mylist

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╰ゝ天使的微笑 2024-10-20 02:21:47

你设置

 oldPerm = Ordering@Ordering@mylist

然后

 newPerm = - oldPerm + Length@mylist + 1

(Reverse@Sort@mylist)[[newPerm]]==mylist

如果
True


所以,你可以定义

newPerm[x_] := 1 + Length@x - Ordering@Ordering@x

比如

(Reverse@Sort@mylist)[[newPerm[mylist]]] == mylist  

is True

If you set

 oldPerm = Ordering@Ordering@mylist

then

 newPerm = - oldPerm + Length@mylist + 1

and

(Reverse@Sort@mylist)[[newPerm]]==mylist

is
True


So, you may define

newPerm[x_] := 1 + Length@x - Ordering@Ordering@x

Such as

(Reverse@Sort@mylist)[[newPerm[mylist]]] == mylist  

is True

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文