将 Mathematica 值列表转换为布尔列表

发布于 2024-12-14 01:34:12 字数 885 浏览 3 评论 0原文

首先,对混乱的标题表示歉意。

我想要做的是将 {1, 4, 9} 转换为:

{True, False, False, True, False, False, False, False, True}

也就是说,只有第一个列表中的索引才会具有值 True,其余的将为错误

我感觉有一些非常简单的解决方案,但我对 Mathematica 和函数式编程都很陌生。我可以在循环中迭代地执行此操作,但必须有一些东西可以与整个列表一起使用。正确的? :)

感谢您的帮助。


编辑:为了表明我在提出要求之前尝试做某事,这是迄今为止我的进展:

first={1,4,9}
ReplacePart[Table[False, {x, Max[first]}], {1} -> True]
(* gives {True, False, False, False, False, False, False, False, False} *)

不幸的是,它不适用于 {1,4,9} ->  True,但可以与 {1 ->;正确,4 ->确实,9 ->正确}。但我不知道如何做到这一点...


编辑2:明白了。

ReplacePart[Table[False, {x, Max[first]}], Table[x -> True, {x, first}]]

我仍然很想看到您的解决方案!这对我来说似乎是一个丑陋的黑客......:)

First of all, sorry for the confused title.

What I want to do is to convert {1, 4, 9} to:

{True, False, False, True, False, False, False, False, True}

That is, only the indexes from the first list will have value True, the rest will be False.

I sense there is some really simple solution, but I am quite new to both Mathematica and functional programming. I could do it iteratively, in a loop, but there has to be something that works with the list as a whole. Right? :)

Thanks for your help.


EDIT: to show that I tried to do something before I asked, here's my progress so far:

first={1,4,9}
ReplacePart[Table[False, {x, Max[first]}], {1} -> True]
(* gives {True, False, False, False, False, False, False, False, False} *)

Unfortunately, it doesn't work with {1,4,9} -> True, but would work with {1 -> True, 4 -> True, 9 -> True}. But I don't know how to get to that...


EDIT 2: got it.

ReplacePart[Table[False, {x, Max[first]}], Table[x -> True, {x, first}]]

I'd still love to see your solutions! This one seems like an ugly hack to me ... :)

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

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

发布评论

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

评论(3

烛影斜 2024-12-21 01:34:12

这是一个简单的方法:

first = {1, 4, 9};
list = ConstantArray[False, Max@first];
list[[first]] = True;

list
Out[1]= {True, False, False, True, False, False, False, False, True}

这是将上述解决方案编写为一个方便的函数:

Clear[convertIndices]
convertIndices[index_List] := 
 Module[{list = ConstantArray[False, Max@index]}, 
  list[[index]] = True; list]

用法:

convertIndices@{1, 4, 9}
Out[2]= {True, False, False, True, False, False, False, False, True}

Here's a simple approach:

first = {1, 4, 9};
list = ConstantArray[False, Max@first];
list[[first]] = True;

list
Out[1]= {True, False, False, True, False, False, False, False, True}

Here's the above solution written as a convenient function:

Clear[convertIndices]
convertIndices[index_List] := 
 Module[{list = ConstantArray[False, Max@index]}, 
  list[[index]] = True; list]

Usage:

convertIndices@{1, 4, 9}
Out[2]= {True, False, False, True, False, False, False, False, True}
镜花水月 2024-12-21 01:34:12

我将使用 SparseArray 来执行此操作。在我看来,它非常容易理解,而且也很高效,特别是当索引为 True 的比例较低时。

true = {1, 4, 9};
SparseArray[(List /@ true) -> True, Automatic, False]

或者使用 Transpose(粘贴到 Mathematica 中时看起来更好):

SparseArray[{true}\[Transpose] -> True, Automatic, False]

如果必须将输出转换为普通数组,则可以使用 Normal,但是大多数操作不需要这样做。


另外,为了简洁而牺牲实用性:

#==1 & /@ SparseArray[List /@ true -> 1]

I would use SparseArray for this operation. In my opinion it is very easy to understand, and it is also efficient, especially when a low percentage of indices are True.

true = {1, 4, 9};
SparseArray[(List /@ true) -> True, Automatic, False]

Alternatively with Transpose (which looks better when pasted into Mathematica):

SparseArray[{true}\[Transpose] -> True, Automatic, False]

You can use Normal if you must convert the output to a normal array, but most operations will not require that.


Also, sacrificing practicality for terseness:

#==1 & /@ SparseArray[List /@ true -> 1]
等数载,海棠开 2024-12-21 01:34:12

实际上,我自己会使用 Yoda 的答案,但这里有一种替代方法:

first = {1, 4, 9};
MemberQ[first, #] & /@ Range[Max[first]]

(* ===> {True, False, False, True, False, False, False, False, True}*)

或者这个:

Or @@@ Outer[Equal, Range[Max[first]], first]


(* ===> {True, False, False, True, False, False, False, False, True}*)

两者都有跳过 Yoda 的 ConstantArray 初始化步骤的优点。

Actually, I would have used Yoda's answer myself, but here's an alternative:

first = {1, 4, 9};
MemberQ[first, #] & /@ Range[Max[first]]

(* ===> {True, False, False, True, False, False, False, False, True}*)

Or this one:

Or @@@ Outer[Equal, Range[Max[first]], first]


(* ===> {True, False, False, True, False, False, False, False, True}*)

Both have the advantage that they skip Yoda's ConstantArray initialization step.

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