Julia中有外部地图功能吗? [英] Is there outer map function in Julia?

查看:21
本文介绍了Julia中有外部地图功能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建四个向量(模型中的参数)的所有可能组合,这会给我一个大的 nx4 矩阵,然后我可以对每组(行)参数运行模拟.在 RI 中,可以通过使用 Mathematica 风格的 expand.grid 来实现这一点,我可以使用类似于 vcat 的外部产品,并使用 hcat 减少输出.

I am trying to construct all possible combinations of four vectors (parameters in a model) that would give me a big nx4 matrix and I could then run simulation on each set (row) of parameters. In R I would achieve this by using expand.grid in Mathematica style, I could use something like outer product with vcat and reduce the output using hcat.

是否有一些类似于 R 或外部映射函数的 expand.grid 的函数?

Is there some function analog of expand.grid from R or outer map function?

玩具示例:

A = [1 2]
B = [3 4]

some magic

output = [1 3, 1 4, 2 3, 2 4]

推荐答案

使用 Iterators 包,它可能看起来像这样:

Using the Iterators package, it might look like this:

using Iterators
for p in product([1,2], [3,4])
    println(p)
end

您将在哪里用您的算法替换 println.如果获取所有组合的集合很重要,您也可以使用 collect.

where you would replace println with your algorithm. You can also use collect if it's important to get the set of all combinations.

这篇关于Julia中有外部地图功能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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