按R中的虚部对复数向量进行排序 [英] sorting a complex vector by imaginary part in R

查看:153
本文介绍了按R中的虚部对复数向量进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

roots <- polyroot(c(5, 4, 3, 2, 1))

我想按虚部的升序对根进行排序.默认情况下,sort函数按实部的升序对其进行排序.我已经阅读了文档,但仍然不知道如何调整参数或编写命令.有人可以帮忙吗?

I want to sort the roots by ascending order of the imaginary part. By default the sort function sorts it by the increasing order of the real part. I've read the document and I still do not know how to adjust the arguments or write the command. Can anybody help?

推荐答案

使用Im函数提取虚部并对其进行排序.

Use Im function to extract the imaginary part and sort it.

roots <- polyroot(c(5, 4, 3, 2, 1))
#[1]  0.287815+1.416093i -1.287815+0.857897i -1.287815-0.857897i
#[4]  0.287815-1.416093i

roots[order(Im(roots))]
#[1]  0.287815-1.416093i -1.287815-0.857897i -1.287815+0.857897i
#[4]  0.287815+1.416093i

这篇关于按R中的虚部对复数向量进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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