Octave:如何将整数向量转换为字符串元胞数组?
Octave: How to turn a vector of integers into a cell array of strings?
八度:
给定一个向量
a = 1:3
如何将向量“a”转换为字符串元胞数组
{'1','2','3'}
(此输出:)
ans =
{
[1,1] = 1
[1,2] = 2
[1,3] = 3
}
(问题结束)
更多信息:我的目标是使用a作为strcat('x',a)
的输入得到
ans =
{
[1,1] = x1
[1,2] = x2
[1,3] = x3
}
欢迎使用其他解决方案来实现这一目标,但主要问题是关于直接从向量中获取字符串数组。
这个问题是 cell array, add suffix to every string 的后续问题,对我没有帮助,因为 strcat('x',{'1','2','3'})
有效,但 strcat('x', num2cell(1:3))
无效:
>> strcat('x', num2cell(1:3))
warning: implicit conversion from numeric to char
warning: called from
strcat at line 121 column 8
C:/Users/USERNAME/AppData/Local/Temp/octave/octave_ENarag.m at line 1 column 1
warning: implicit conversion from numeric to char
warning: called from
strcat at line 121 column 8
C:/Users/USERNAME/AppData/Local/Temp/octave/octave_ENarag.m at line 1 column 1
warning: implicit conversion from numeric to char
warning: called from
strcat at line 121 column 8
C:/Users/USERNAME/AppData/Local/Temp/octave/octave_ENarag.m at line 1 column 1
ans =
{
[1,1] = x[special sign "square"]
[1,2] = x[special sign "square"]
[1,3] = x[special sign "square"]
}
此外,根据的Matlab函数string(a)
尚未实现:
"The 'string' function is not yet implemented in Octave.`
您可以组合 cellstr
和 int2str
:
strcat('x', cellstr(int2str((1:3).')))
or
cellstr(strcat('x', int2str((1:3).')))
您还可以结合使用 sprintf
和 ostrsplit
:
ostrsplit(sprintf('x%d ', 1:3), ' ', true)
or
strcat('x', ostrsplit(sprintf('%d ', 1:3), ' ', true))
您也可以使用 num2str
代替 int2str
。
只是为了添加另一个 'obvious' 解决方案
arrayfun( @(x) strcat('x', num2str(x)), a, 'UniformOutput', false )
或等效
arrayfun( @(x) sprintf('x%d', x), a, 'UniformOutput', false )
八度:
给定一个向量
a = 1:3
如何将向量“a”转换为字符串元胞数组
{'1','2','3'}
(此输出:)
ans =
{
[1,1] = 1
[1,2] = 2
[1,3] = 3
}
(问题结束)
更多信息:我的目标是使用a作为strcat('x',a)
的输入得到
ans =
{
[1,1] = x1
[1,2] = x2
[1,3] = x3
}
欢迎使用其他解决方案来实现这一目标,但主要问题是关于直接从向量中获取字符串数组。
这个问题是 cell array, add suffix to every string 的后续问题,对我没有帮助,因为 strcat('x',{'1','2','3'})
有效,但 strcat('x', num2cell(1:3))
无效:
>> strcat('x', num2cell(1:3))
warning: implicit conversion from numeric to char
warning: called from
strcat at line 121 column 8
C:/Users/USERNAME/AppData/Local/Temp/octave/octave_ENarag.m at line 1 column 1
warning: implicit conversion from numeric to char
warning: called from
strcat at line 121 column 8
C:/Users/USERNAME/AppData/Local/Temp/octave/octave_ENarag.m at line 1 column 1
warning: implicit conversion from numeric to char
warning: called from
strcat at line 121 column 8
C:/Users/USERNAME/AppData/Local/Temp/octave/octave_ENarag.m at line 1 column 1
ans =
{
[1,1] = x[special sign "square"]
[1,2] = x[special sign "square"]
[1,3] = x[special sign "square"]
}
此外,根据string(a)
尚未实现:
"The 'string' function is not yet implemented in Octave.`
您可以组合 cellstr
和 int2str
:
strcat('x', cellstr(int2str((1:3).')))
or
cellstr(strcat('x', int2str((1:3).')))
您还可以结合使用 sprintf
和 ostrsplit
:
ostrsplit(sprintf('x%d ', 1:3), ' ', true)
or
strcat('x', ostrsplit(sprintf('%d ', 1:3), ' ', true))
您也可以使用 num2str
代替 int2str
。
只是为了添加另一个 'obvious' 解决方案
arrayfun( @(x) strcat('x', num2str(x)), a, 'UniformOutput', false )
或等效
arrayfun( @(x) sprintf('x%d', x), a, 'UniformOutput', false )