我有一个名为skj的数组skj包含200万行数字(2000000x1uint32)。
我想计算一下

string_skj = num2str(skj);

当我运行上面的线路时,大约需要1分钟,有没有一种更快的方法来完成它?

最佳答案

Hennadii Madan的答案让我想到,如果有一种方法能比标准的Matlab(或num2str)更有效地处理列向量,我已经提出了两种解决方案。
编辑:在所有的工作结束后,@Luis Mendo进来,把它从水里吹出来:'(
编辑:现在@Daniel在以前所有选项的基础上又有了改进!
给定我们的行向量,int2str,作为

V = uint32(randi(100, 200000, 1));

我们可以达到同样的效果
A = num2str(V);

与*
B = char(strsplit(num2str(V.')).');

或不进行V的错误检查
C = char(strsplit(sprintf('%d\n', V)).');
C = C(1:end-1, :); % Remove extraneous '\n'

num2strBC略有不同A带有空格的前焊盘,num2str,而' 'B带有空格的后焊盘。
在下面的CD中预先填充了E,因此与0AB不完全匹配。
基准点
-----num2str() on row vector [Original]-----
Elapsed time is 3.501976 seconds.
  Name           Size              Bytes  Class    Attributes

  A         200000x3             1200000  char

-----num2str() on column vector [IKavanagh modified from Hennadii Madan]-----
Elapsed time is 0.660878 seconds.
  Name           Size              Bytes  Class    Attributes

  B         200000x3             1200000  char

-----sprintf() on row vector [IKavanagh]-----
Elapsed time is 0.582472 seconds.
  Name           Size              Bytes  Class    Attributes

  C         200000x3             1200000  char

-----dec2base() on row vector [Luis Mendo]-----
Elapsed time is 0.042563 seconds.
  Name           Size              Bytes  Class    Attributes

  D         200000x3             1200000  char



-----myfastint2str() on row vector [Daniel]-----
Elapsed time is 0.011894 seconds.
  Name           Size              Bytes  Class    Attributes

  E         200000x3             1200000  char

代码
clear all
close all
clc

V = uint32(randi(100, 200000, 1));

for k = 1:50000
    tic(); elapsed = toc(); % Warm up tic/toc
end

disp('-----num2str() on row vector [Original]-----');
tic;
A = num2str(V);
toc, whos A

disp('-----num2str() on column vector [IKavanagh modified from Hennadii Madan]-----');
tic;
B = char(strsplit(num2str(V.')).');
toc, whos B

disp('-----sprintf() on row vector [IKavanagh]-----');
tic;
C = char(strsplit(sprintf('%d\n', V)).');
C = C(1:end-1, :); % Remove extraneous '\n'
toc, whos C

disp('-----dec2base() on row vector [Luis Mendo]-----');
tic;
D = dec2base(V, 10);
toc, whos D

disp('-----myfastint2str() on row vector [Daniel]-----');
tic;
E = myfastint2str(V);
toc, whos E

想法转换的功劳应该归于Hennadii Madan

10-02 04:58