重塑视图功能? [英] Function for Reshape View?
问题描述
在Julia v0.5中,您如何制作类似于重塑形状但返回视图的函数? 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屋!