作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设我有一个 Array
变量名为 p
:
julia> p = [5]
julia> typeof(p)
Array{Int64,1}
p
也可能是二维的:
julia> p = [1]''
julia> typeof(p)
Array{Int64,2}
p
任何维度,但我应该如何将其减少到标量?
p=p[1]
, 但如果
p
不会抛出任何错误在
p
中有多个元素;所以,这对我没有好处。
function scalar(x)
assert(length(x) == 1)
x[1]
end
squeeze
,它只是剥离尺寸直到
p
是一个零维数组。
最佳答案
如果您想获取标量但如果数组形状错误则抛出错误,您可以 reshape
:
julia> p1 = [4]; p2 = [5]''; p0 = []; p3 = [6,7];
julia> reshape(p1, 1)[1]
4
julia> reshape(p2, 1)[1]
5
julia> reshape(p0, 1)[1]
ERROR: DimensionMismatch("new dimensions (1,) must be consistent with array size 0")
in reshape at array.jl:122
in reshape at abstractarray.jl:183
julia> reshape(p3, 1)[1]
ERROR: DimensionMismatch("new dimensions (1,) must be consistent with array size 2")
in reshape at array.jl:122
in reshape at abstractarray.jl:183
关于arrays - 我应该如何将单例数组转换为标量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29159386/
我最近购买了《C 编程语言》并尝试了 Ex 1-8这是代码 #include #include #include /* * */ int main() { int nl,nt,nb;
早上好!我有一个变量“var”,可能为 0。我检查该变量是否为空,如果不是,我将该变量保存在 php session 中,然后调用另一个页面。在这个新页面中,我检查我创建的 session 是否为空,
我正在努力完成 Learn Python the Hard Way ex.25,但我无法理解某些事情。这是脚本: def break_words(stuff): """this functio
我是一名优秀的程序员,十分优秀!