将 Julia 中大小为 1*N 或 N*1 的 Matrix{T} 转换为 Vector{T} 的最有效方法是什么? [英] What's the most efficient way to convert a Matrix{T} of size 1*N or N*1 in Julia to a Vector{T}?

查看:14
本文介绍了将 Julia 中大小为 1*N 或 N*1 的 Matrix{T} 转换为 Vector{T} 的最有效方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将 Julia 中大小为 1*N 或 N*1 的 Matrix{T} 转换为 Vector{T} 的最有效方法是什么?

What's the most efficient way to convert a Matrix{T} of size 1*N or N*1 in Julia to a Vector{T}?

比如说我有

a = [1,3,5]
b = a'

ab 都是 Array{Int,2} 类型(即 Matrix{Int}).将 ab 转换为 Array{Int,1} 类型(即 Vector{Int})?

Both a and b are of type Array{Int,2} (i.e. Matrix{Int}). What are the most efficient ways to convert a and b to type Array{Int,1} (i.e. Vector{Int})?

一种方法是:

a_vec = [x::Int for x in a]
b_vec = [x::Int for x in b]

推荐答案

您可以使用 vec() 函数.它比列表理解更快,并且随着元素数量的增加而更好地扩展;)对于 1000x1 的矩阵:

You can use the vec() function. It's faster than the list comprehension and scales better with number of elements ;) For a matrix of 1000x1:

julia> const a = reshape([1:1000],1000,1);

julia> typeof(a)
Array{Int64,2}

julia> vec_a = [x::Int for x in a];

julia> typeof(vec_a)
Array{Int64,1}

julia> vec_aII = vec(a);

julia> typeof(vec_aII)
Array{Int64,1}

6.41e-6 seconds # 列表理解

6.41e-6 seconds # list comprehension

2.92e-7 秒 #vec()

这篇关于将 Julia 中大小为 1*N 或 N*1 的 Matrix{T} 转换为 Vector{T} 的最有效方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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