Octave:如何将整数向量转换为字符串元胞数组? [英] Octave: How to turn a vector of integers into a cell array of strings?
问题描述
八度:
给定一个向量
a = 1:3
如何转换矢量a"成一个字符串元胞数组
How to turn vector "a" into a cell array of strings
{'1','2','3'}
(输出:)
ans =
{
[1,1] = 1
[1,2] = 2
[1,3] = 3
}
(问题结束)
进一步的信息:我的目标是使用a作为strcat('x',a)
的输入来得到
Further information: my aim is to use a as the input of strcat('x',a)
to get
ans =
{
[1,1] = x1
[1,2] = x2
[1,3] = x3
}
欢迎使用其他解决方案来实现这一目标,但主要问题是直接从向量中获取字符串数组.
Other solutions how to reach this aim are welcome, but the main question is about getting an array of strings directly from a vector.
这个问题是元胞数组的后续问题,给每一个添加后缀string 对我没有帮助,因为 strcat('x',{'1','2','3'})
有效,但是 strcat('x', num2cell(1:3))
不会:
This question is a follow-up from cell array, add suffix to every string which could not help me, since strcat('x',{'1','2','3'})
works, but strcat('x', num2cell(1:3))
does not:
>> 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)
根据 Matlab:如何将元胞数组转换为字符串数组?,尚未实现:
Also, the Matlab function string(a)
according to Matlab: How to convert cell array to string array?, is not yet implemented:
"'string' 函数还没有在 Octave 中实现.`
"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
.
You can also use num2str
instead of int2str
.
这篇关于Octave:如何将整数向量转换为字符串元胞数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!