String matlab向数值向量的每个元素添加字符串

String matlab向数值向量的每个元素添加字符串,string,matlab,concatenation,String,Matlab,Concatenation,我需要在数字向量和字符串向量之间切换: a = [1, 2, 4, 5] b = {'xx1', 'xx2', 'xx3', 'xx4'} 我尝试了strcat('XX',num2str(a)),但它返回XX1 2 3 4 5,这不是我想要的。有人能告诉我如何从a创建b以及如何从b创建a吗?谢谢 解决方案1 没有循环,您可以使用cellfun: xxs = cell(1,4); xxs(:) = 'XX'; q = cell(1,4); q{1}=1;q{2}=2;q{3}=4;q{4}=5;

我需要在数字向量和字符串向量之间切换:

a = [1, 2, 4, 5]
b = {'xx1', 'xx2', 'xx3', 'xx4'}
我尝试了strcat('XX',num2str(a)),但它返回
XX1 2 3 4 5
,这不是我想要的。有人能告诉我如何从
a
创建
b
以及如何从
b
创建
a
吗?谢谢

解决方案1 没有循环,您可以使用
cellfun

xxs = cell(1,4);
xxs(:) = 'XX';
q = cell(1,4); q{1}=1;q{2}=2;q{3}=4;q{4}=5;
result = cellfun(@(x,i) strcat(x, num2str(i)), xxs,q, 'UniformOutput', false);
解决方案2 使用循环:

a = [1 2 4 5];
result = cell(1,length(a));
for i = 1:length(a)
     result{i} = strcat('XX',num2str(a(i)));
end 

如果您使用循环而不是向量化函数来打开,那么可以相当容易地完成。例如:

clc; clear;

a = [1, 2, 4, 5];
for i = 1:length(a)
    b{i} = strcat('XX', num2str(a(i)));
end
b
返回

b =

  1×4 cell array

{'XX1'}    {'XX2'}    {'XX4'}    {'XX5'}
如果你想走另一条路,你可以这样做

for i = 1:length(b)
    c(i) = str2double(b{i}(3:end));
end
c
返回

c =

     1     2     4     5

这里值得注意的是,更好的代码既可以是
b
也可以是
c

无需循环或
cellfun
。让

a = [1, 2, 4, 15];
b = {'xx1', 'xx2', 'xx4', 'xx15'};
b
a
使用with format说明符
'%-i'
a
转换为字符串单元格数组,以利用忽略尾随空格的事实;使用后一种连接方式后,通过:

a
b
使用从每个字符串中删除
xx
,然后应用:


B来自A

我喜欢使用一个没有文档记录的Matlab函数,它在这种情况下工作得非常完美:
sprintfc
。它的速度非常快,允许使用自定义格式将数字向量转换为字符数组的单元向量:

a = [1, 2, 4, 5];
b = sprintfc('xx%d',a)

b =
    1×4 cell array
    'xx1'   'xx2'   'xx4'   'xx5'
A来自B

使用替换项非常简单:

b = {'xx1', 'xx2', 'xx3', 'xx4'};
a = str2double(strrep(b,'xx',''))

a =
    1     2     3     4

在16b中,MATLAB添加了一个新的字符串类,在17a中,您可以从双引号构造字符串。使用字符串

"xx" + a
我会给你想要的答案。以下是string与其他解决方案的性能比较:

>> profFunc
matlabbit solution: 0.29956
Luis Mendo solution: 15.2378
OmG solution 1: 41.0055
OmG solution 2: 29.1976
Tommaso Belluzzo solution: 1.3574


function  profFunc

    a = [1, 2, 4, 5];

    n = 1E5;

    tic;
    for i = 1:n
        ans = "xx" + a;
    end
    disp("matlabbit solution: " + toc);

    tic;
    for i = 1:n
        ans = strcat('xx', cellstr(num2str(a(:), '%-i'))).';
    end
    disp("Luis Mendo solution: " + toc);

    tic;
    for i = 1:n
        xxs = cell(1,4);
        xxs(:) = {'XX'};
        q = cell(1,4); q{1}=1;q{2}=2;q{3}=4;q{4}=5;
        ans = cellfun(@(x,i) strcat(x, num2str(i)), xxs,q, 'UniformOutput', false);
    end
    disp("OmG solution 1: " + toc);

    tic;
    for i = 1:n
        ans = cell(1,length(a));
        for i = 1:length(a)
             ans{i} = strcat('XX',num2str(a(i)));
        end 
    end
    disp("OmG solution 2: " + toc);

    tic;
    for i = 1:n
        ans = sprintfc('xx%d',a);
    end
    disp("Tommaso Belluzzo solution: " + toc);
end

谢谢我希望能找到一些非循环的方法。经过大量的互联网搜索,显然strcat('XX',string(a))将适用于matlab 2016以后的版本。不幸的是我的是2015年。。。所以我想我会坚持使用loopsjust作为参考,以防有人出现在这个页面上:代码对我也不起作用(可能又是matlab 2015的问题),我必须使用strcat('XX',num2str(a(i)))instead@ian_chan你也可以找到一个没有循环的解决方案。谢谢。我希望能找到一些非循环的方法。经过大量的互联网搜索,显然strcat('XX',string(a))将适用于matlab 2016以后的版本。不幸的是我的是2015年。。。所以我想我会坚持使用loopsin16b中添加的compose函数,它与sprintfc非常相似,但有文档记录。
"xx" + a
>> profFunc
matlabbit solution: 0.29956
Luis Mendo solution: 15.2378
OmG solution 1: 41.0055
OmG solution 2: 29.1976
Tommaso Belluzzo solution: 1.3574


function  profFunc

    a = [1, 2, 4, 5];

    n = 1E5;

    tic;
    for i = 1:n
        ans = "xx" + a;
    end
    disp("matlabbit solution: " + toc);

    tic;
    for i = 1:n
        ans = strcat('xx', cellstr(num2str(a(:), '%-i'))).';
    end
    disp("Luis Mendo solution: " + toc);

    tic;
    for i = 1:n
        xxs = cell(1,4);
        xxs(:) = {'XX'};
        q = cell(1,4); q{1}=1;q{2}=2;q{3}=4;q{4}=5;
        ans = cellfun(@(x,i) strcat(x, num2str(i)), xxs,q, 'UniformOutput', false);
    end
    disp("OmG solution 1: " + toc);

    tic;
    for i = 1:n
        ans = cell(1,length(a));
        for i = 1:length(a)
             ans{i} = strcat('XX',num2str(a(i)));
        end 
    end
    disp("OmG solution 2: " + toc);

    tic;
    for i = 1:n
        ans = sprintfc('xx%d',a);
    end
    disp("Tommaso Belluzzo solution: " + toc);
end