Matlab,如何将整数字符串转换为向量? [英] Matlab, how to convert a string of integers into a vector?

查看:2059
本文介绍了Matlab,如何将整数字符串转换为向量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
将字符串转换为matlab中的数字数组

Possible Duplicate:
convert string to number array in matlab

Matlab中是否有一种简单的方法来转换这样的字符串

Is there a simple way in Matlab to convert a string like this

'123456789'

变成这样的向量?

[1 2 3 4 5 6 7 8 9]

推荐答案

如果您所拥有的都是从0到9的连续字符:

If all you have is contiguous characters from 0 to 9:

v = double(s)-'0';

double将字符串转换成数组,其中每个元素都是相应字符的ASCII码.为了获得数字值,我们减去"0"(实际上是ASCII中的48),并且由于数字在ASCII代码中具有顺序表示形式("1" = 49,"2" = 50,依此类推),因此最终得到了预期结果结果.

double(s) converts a string into an array where each element is the ASCII code of the corresponding character. To obtain the numberic values we subtract '0' (which is in fact 48 in ASCII) and since digits have a sequential representation in ASCII code ('1' = 49, '2' = 50, etc.) we end up with intended result.

这篇关于Matlab,如何将整数字符串转换为向量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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