且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Matlab,如何将整数字符串转换为向量?

更新时间:2023-01-16 17:45:06

如果您所拥有的都是从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.