重塑视图的功能? [英] Function for Reshape View?
问题描述
在 Julia v0.5 中,如何创建一个类似于 reshape 但返回视图的函数?ArrayViews.jl 有一个 reshape_view 函数,但它似乎与新的 view
函数不直接兼容.我只想 reshape
u
到一些我不知道尺寸的元组 sizeu
.
In Julia v0.5, how do you make a function which is like reshape but instead returns a view? ArrayViews.jl has a reshape_view function but it doesn't seem directly compatible with the new view
function. I just want to reshape
u
to some tuple sizeu
where I don't know the dimensions.
推荐答案
如果你重塑一个视图",输出就是一个重塑后的视图".
If you reshape a 'view', the output is a reshaped 'view'.
如果您的初始变量是一个普通数组,您可以在函数调用期间将其转换为动态"视图.
If your initial variable is a normal array, you can convert it to a view 'on the fly' during your function call.
根据您后来的评论,此操作期间没有重新分配:您可以使用 pointer
函数确认这一点.对象并不相同,因为它们被解释为指向不同类型"的指针,但内存地址是相同的.
There are no reallocations during this operation, as per your later comment: you can confirm this with the pointer
function. The objects aren't the same, in the sense that they are interpreted as pointers to a different 'type', but the memory address is the same.
julia> A = ones(5,5,5); B = view(A, 2:4, 2:4, 2:4); C = reshape(B, 1, 27);
julia> is(B,C)
false
julia> pointer(B)
Ptr{Float64} @0x00007ff51e8b1ac8
julia> pointer(C)
Ptr{Float64} @0x00007ff51e8b1ac8
julia> C[1:5] = zeros(1,5);
julia> A[:,:,2]
5×5 Array{Float64,2}:
1.0 1.0 1.0 1.0 1.0
1.0 0.0 0.0 1.0 1.0
1.0 0.0 0.0 1.0 1.0
1.0 0.0 1.0 1.0 1.0
1.0 1.0 1.0 1.0 1.0
这篇关于重塑视图的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!