将向量元素作为单个整数返回 [英] Return vector elements as a single integer
问题描述
在 MATLAB 中,如何将向量的内容作为单个整数返回.例如,如果 a = [ 1 2 3 4 5 6 7 8 9 ]
,那么我想要一个返回数字 123456789
的函数.
In MATLAB, how can I return the contents of a vector as a single integer. For example, if a = [ 1 2 3 4 5 6 7 8 9 ]
, then I want a function that returns the number 123456789
.
几乎是挤压但去除了所有尺寸!!
It's almost squeeze but removing all dimensions!!
我目前的解决方案是使用 sprintf
以 '%i'
格式将向量作为字符串读取,然后使用 str2double 将其转换为数字代码>.如果你只需要使用它几次就可以了,但是如果它被使用 100000 次就相当低效了.
My current solution is to read the vector as a string using sprintf
with a format of '%i'
, then convert it to a number using str2double
. That's ok if you only need to use it a few times, but fairly inefficient if it's being used 100000 times.
推荐答案
可以使用
result = a*10.^(numel(a)-1:-1:0).';
或等效
result = sum(a.*10.^(numel(a)-1:-1:0));
它们的速度似乎差不多.
They seem to be about equally fast.
这篇关于将向量元素作为单个整数返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!