Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert from double to strings

I have the matrix of double 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];

and I want to convert it in a matrix of string B

B= {'11121';
    '21121';
    '31121';
    '41121';
    '12121';
    '22121';
    '32121';
    '42121'}.

To do this, I have tried to use num2str but I obtained C that inside every string have two spaces

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'} 

I don't know how to delete the spaces from C.

like image 598
elis56 Avatar asked May 19 '26 03:05

elis56


2 Answers

One way to do this is to use sprintf to convert the array to a long string of digits. You can then reshape this string into the appropriate shape. Then you can use cellstr to convert each row of the reshaped string into a separate cell array element.

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

Explanation

First convert the matrix into a long string of digits.

s = sprintf('%d', A)
%// 1234123411112222111111112222222211111111 

Then we want to reshape this so that each row of numbers in the original is a row of numbers in the output

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

Then we can use cellstr to convert each row of this into it's own cell array

out = cellstr(s);
%// '11121'
%// '21121'
%// '31121'
%// '41121'
%// '12121'
%// '22121'
%// '32121'
%// '42121'

A different approach

Another way that you could accomplish this is to treat each column of A as a place value (i.e. 10000's place, 1000's, place, 100's place, etc.) and convert each row to an integer knowing that. This can easily be done by multiplying each row with an array of 10^(N-1:-1:0) and summing the elements. This will yield a digit for each row that combines all of the columns. We can then use num2str to convert this to a cell array of strings.

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

Or to shorten this even more, we can borrow a page out of @rayryeng's book and use sprintfc to convert this array of integers into a cell array of strings:

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

Benchmark

I was curious about the performance of the methods presented here and in @rayryeng's answer and Dev-iL's answer when you increase the number of rows. I wrote up a quick test script.

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

Here are the results.

enter image description here

like image 111
Suever Avatar answered May 20 '26 21:05

Suever


My suggestion is this:

out = cellstr(char(A + '0'));

Basically what we do is add the ASCII value of 0 to your matrix then convert it to characters. I didn't benchmark it, but it should be comparably fast :)

like image 29
Dev-iL Avatar answered May 20 '26 19:05

Dev-iL



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!