从double转换为字符串

eli*_*s56 2 string double matlab

我有双重矩阵 A

  A=[1 1 1 2 1;
     2 1 1 2 1;
     3 1 1 2 1;
     4 1 1 2 1;
     1 2 1 2 1;
     2 2 1 2 1;
     3 2 1 2 1;
     4 2 1 2 1];
Run Code Online (Sandbox Code Playgroud)

我想用字符串矩阵转换它 B

B= {'11121';
    '21121';
    '31121';
    '41121';
    '12121';
    '22121';
    '32121';
    '42121'}.
Run Code Online (Sandbox Code Playgroud)

为此,我尝试使用num2str但是我获得C了每个字符串内部有两个空格

C = {'1  1  1  2  1';
     '2  1  1  2  1';
     '3  1  1  2  1';
     '4  1  1  2  1';
     '1  2  1  2  1';
     '2  2  1  2  1';
     '3  2  1  2  1';
     '4  2  1  2  1'} 
Run Code Online (Sandbox Code Playgroud)

我不知道如何删除空格C.

Sue*_*ver 6

一种方法是使用sprintf将数组转换为长数字串.然后,您可以将此字符串重塑为适当的形状.然后,您可以使用cellstr将重新整形的字符串的每一行转换为单独的单元格数组元素.

out = cellstr(reshape(sprintf('%d', A), [], size(A,2)));
Run Code Online (Sandbox Code Playgroud)

说明

首先将矩阵转换为长数字串.

s = sprintf('%d', A)
%// 1234123411112222111111112222222211111111 
Run Code Online (Sandbox Code Playgroud)

然后我们想重塑这个,以便原始数字中的每一行都是输出中的一行数字

s = reshape(s, [], size(A,2))
%// 11121
%// 21121
%// 31121
%// 41121
%// 12121
%// 22121
%// 32121
%// 42121
Run Code Online (Sandbox Code Playgroud)

然后我们可以使用cellstr它将每行转换为它自己的单元格数组

out = cellstr(s);
%// '11121'
%// '21121'
%// '31121'
%// '41121'
%// '12121'
%// '22121'
%// '32121'
%// '42121'
Run Code Online (Sandbox Code Playgroud)

一种不同的方法

另一种可以实现此目的的方法是将每列A作为一个位置值(即10000的位置,1000位,位置,100位等),并将每一行转换为一个整数,知道这一点.这可以通过将每行乘以10^(N-1:-1:0)元素的数组和求和来轻松完成.这将为组合所有列的每一行产生一个数字.然后我们可以使用num2str它将其转换为字符串的单元格数组.

%// Then convert each number to a string in a cell array
out = arrayfun(@num2str, A * (10.^(size(A, 2)-1:-1:0)).', 'uni', 0);
Run Code Online (Sandbox Code Playgroud)

或者为了进一步缩短这一点,我们可以借用@ rayryeng的书中的页面并使用sprintfc将这个整数数组转换为字符串的单元格数组:

out = sprintfc('%d', A * (10.^(size(A, 2)-1:-1:0)).');
Run Code Online (Sandbox Code Playgroud)

基准

我很好奇这里介绍的方法的性能以及@ rayryeng的答案Dev-iL在增加行数时的答案.我写了一个快速测试脚本.

function tests()
    % Test the number of rows between 100 and 10000
    nRows = round(linspace(100, 10000, 100));

    times1 = zeros(numel(nRows), 1);
    times2 = zeros(numel(nRows), 1);
    times3 = zeros(numel(nRows), 1);
    times4 = zeros(numel(nRows), 1);
    times5 = zeros(numel(nRows), 1);

    %// Generate a random matrix of N x 5
    getRandom = @(n)randi([0, 9], [n, 5]);

    for k = 1:numel(nRows)
        A = getRandom(nRows(k));
        times1(k) = timeit(@()string_reshape_method(A));
        A = getRandom(nRows(k));
        times2(k) = timeit(@()base10_method(A));
        A = getRandom(nRows(k));
        times3(k) = timeit(@()sprintfc_method(A));
        A = getRandom(nRows(k));
        times4(k) = timeit(@()addition_method(A));
    end

    %// Plot the results
    plot(nRows, cat(2, times1, times2, times3, times4)*1000);
    legend({'String Reshape', 'Base-10 Conversion', 'sprintfc', 'addition of "0"'})

    xlabel('Number of Rows in A')
    ylabel('Execution Time (ms)');
end

function out = string_reshape_method(A)
    out = cellstr(reshape(sprintf('%d', A), [], size(A,2)));
end

function out = base10_method(A)
    out = sprintfc('%d', A * (10.^(size(A, 2)-1:-1:0)).');
end

function B = sprintfc_method(A)
    B = sprintfc(repmat('%d', 1, size(A,2)), A);
end

function B = addition_method(A)
    B = cellstr(char(A + '0'));
end
Run Code Online (Sandbox Code Playgroud)

结果如下.

在此输入图像描述


Dev*_*-iL 5

我的建议如下:

out = cellstr(char(A + '0'));
Run Code Online (Sandbox Code Playgroud)

基本上我们所做的是将ASCII值添加0到矩阵然后将其转换为字符.我没有对它进行基准测试,但它应该比较快:)

  • @Dan - 感谢你指出这一点.我过于专注于提供的例子...... (2认同)