MATLAB - 数字转换的字符数组数整数数组/矩阵? [英] MATLAB - Convert Number Character array to Number Integer array/matrix?

查看:147
本文介绍了MATLAB - 数字转换的字符数组数整数数组/矩阵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题:我有一个数组的类型的字符,上面写着这样的:

My problem: I have an array A of type char, that reads like this:

196800271010
902589451069
052068830384
901778191098

我想转成将人物分成个体数量的整数,像这样的数字矩阵

I want to turn A into a numerical matrix that will separate the characters into individual number integers like so

[1 9 6 8 0 0 2 7 1 0 1 0
 9 0 2 5 8 9 4 5 1 0 6 9
 0 5 2 0 6 8 8 3 0 3 8 4
 9 0 1 7 7 8 1 9 1 0 9 8]

什么是最好的方法?
我试过
    str2num(A),但对待字符每行一个整数(即第一行是1.9680)
我也试过这种循环

What is the best way for this? I've tried str2num(A), but that treats the characters as one whole number per line (Ie first line is 1.9680) I've also tried this for loop

for i = 1:5 %the number of rows in the char array
    s = num2str(A(i,:));
    for t = length(s):-1:1
        result(t) = str2num(s(t));
    end
end

但是,这只是返回数组的最后一行,我想有他们。

But this just returns the last line of the array, I want to have all of them.

推荐答案

如果您转换您的矩阵成列向量,每个角色将定位于不同的行,因此,它可以通过 str2num 。然后重塑回:

If you convert your matrix into a column vector, each character will be positioned in a different row, so that it can be handled independently by str2num. Then reshape back:

result = reshape(str2num(A(:)), size(A))

这篇关于MATLAB - 数字转换的字符数组数整数数组/矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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