vector - Julia .重新标记向量

标签 vector indexing wolfram-mathematica julia

我有一个非连续整数向量,我需要将其用作矩阵的索引。为了创建最小尺寸的矩阵,我想通过以下方式重新标记向量的条目:

input = [794, 421, 496, 158, 421, 794, 794, 656, 108, 496, 496, 687, 108, 511, 108, 108, 158, 687, 158, 687]
output = [1, 2, 3, 4, 2, 1, 1, 5, 6, 3, 3, 7, 6, 8, 6, 6, 4, 7, 4, 7]

以下是我如何在 Mathematica 中执行此操作的示例:

labels = DeleteDuplicates[input]
output = Flatten[Position[labels, #] & /@ input]

在 Julia 中执行此操作的有效方法是什么?

谢谢, 柯岩

最佳答案

解决办法是

relabel(v::Vector{Int64}) = indexin(v, unique(v))

关于vector - Julia .重新标记向量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24701917/

相关文章:

r - 使用 2 个逻辑向量 : replace values in first vector to NA according to FALSE values in the other vector 进行向量化操作的问题

c++ - 读取C++中数据类型为Class的.dat文件,然后将每一行存储在Class类型的vector中

c++ - 如何按索引删除 vector 中每 2 个元素的倍数

arrays - Fortran 中数组索引的段错误

wolfram-mathematica - 为什么默认行为像这样?

C++ 将带有指针的对象添加到 Vector 中

indexing - Cassandra 中的 SASI 索引

Python进程和MySQL

wolfram-mathematica - Mathematica 中的交 fork 列表

wolfram-mathematica - 在 PackedArray 上,寻找使用它们的建议