返回向量元素作为一个整数 [英] Return vector elements as a single integer

查看:129
本文介绍了返回向量元素作为一个整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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 。如果它被用10万次的确定,如果你只需要使用它几次,但效率非常低。

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屋!

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