String 从双精度转换为字符串
我的矩阵是doubleString 从双精度转换为字符串,string,matlab,double,String,Matlab,Double,我的矩阵是doubleA 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]; 我想把它转换成字符串B B= {'11121'; '21121'; '31121'; '41121'; '12121'; '22121'; '32121';
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];
我想把它转换成字符串B
B= {'11121';
'21121';
'31121';
'41121';
'12121';
'22121';
'32121';
'42121'}.
为此,我尝试使用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'}
我不知道如何从
C
中删除空格 一种方法是使用将数组转换为长字符串。然后,可以将该字符串重塑为适当的形状。然后可以使用将重塑字符串的每一行转换为单独的单元格数组元素
out = cellstr(reshape(sprintf('%d', A), [], size(A,2)));
解释
首先将矩阵转换为一长串数字
s = sprintf('%d', A)
%// 1234123411112222111111112222222211111111
然后,我们要对其进行重塑,使原始数据中的每一行数字都是输出数据中的一行数字
s = reshape(s, [], size(A,2))
%// 11121
%// 21121
%// 31121
%// 41121
%// 12121
%// 22121
%// 32121
%// 42121
然后我们可以使用cellstr
将每一行转换为它自己的单元格数组
out = cellstr(s);
%// '11121'
%// '21121'
%// '31121'
%// '41121'
%// '12121'
%// '22121'
%// '32121'
%// '42121'
另一种方法
实现这一点的另一种方法是将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);
或者为了进一步缩短时间,我们可以从书中借用一页,使用sprintfc
将这个整数数组转换为字符串单元格数组:
out = sprintfc('%d', A * (10.^(size(A, 2)-1:-1:0)).');
基准
我很好奇这里介绍的方法的性能,以及增加行数时的性能。我写了一个快速测试脚本
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
以下是结果
使用未记录的东西怎么样?我们可以看到,每个单元格有5个数字,或者每个单元格的总列数。同样地,创建一个格式字符串,类似于您在
%d
中使用的fprintf
,但在a
中重复的列,然后使用undocumented函数一次性完成从数字到单元格的转换:
s = repmat('%d', 1, size(A,2));
B = sprintfc(s, A);
示例运行
我的建议是:
out = cellstr(char(A + '0'));
基本上,我们要做的是将0
的ASCII值添加到矩阵中,然后将其转换为字符。我没有对它进行基准测试,但它应该比较快:)基于从num2str
输出中“清除空格”的方法:
方法1:
cellfun(@(s)s(s~=' '), num2str(A));
regexprep(cellstr(num2str(A)),' ','');
方法2:
cellfun(@(s)s(s~=' '), num2str(A));
regexprep(cellstr(num2str(A)),' ','');
@不管怎样,呵呵。。。因此,它值得称为undocumented的函数:D.谢谢。我之所以添加此注释,是因为其他答案可能更好,但您可以轻松地删除
C
中的所有空格,如下所示:cellfun(@(s)s(s~=”),C)
@elis56-只是想知道-您说输入矩阵是双精度的
,但在您的示例中,它包含一位整数。可能输入的实际值是多少?会有分数、复数等吗。?或者它们总是表示单个数字的正整数?如果字符串的长度都相同,为什么需要单元格数组?普通的字符矩阵可能会更快。@elis56如果您对不同方法的性能感兴趣,请务必签出更新的帖子。我现在看到了帖子:感谢您的绘图和建议:)这只适用于小于9的数字though@Dan-谢谢你指出这一点。我太专注于提供的示例了…@Dev iL我认为这是一个公平的假设,而公认的重塑方法可能也有这个局限性