繁体   English   中英

MATLAB:将一维字符串的单元格数组转换为二维字符串

[英]MATLAB: Convert cell array of 1D strings to 2D string

MATLAB的最新版本具有字符串 ,它们是字符向量的N维矩阵。 我有一个这样的1D字符串的单元格数组,我想将它们组合成一个2D字符串,但是这样做很麻烦。 joinstrjoinstrcat函数在字符串内部的字符数组上起作用,而cell2mat不起作用:

>> cell2mat({strings(1, 4); strings(1, 4)})
Error using cell2mat (line 52)
CELL2MAT does not support cell arrays containing cell arrays or objects. 

有什么好办法吗? 我希望上述情况下的输出是2x1 string对象。

string对象与同一类型串联时,其行为与任何其他数据类型( doublechar等)一样。 只要您希望结果也成为string对象,请使用普通串联。

result = [strings(1, 4); strings(1, 4)];

或者您可以使用catvertcat更明确

result = cat(1, strings(1, 4), strings(1, 4));
result = vertcat(strings(1, 4), strings(1, 4));

或者,您可以使用索引对同一元素进行两次采样

result = strings([1 1], 4);

如果您的数据已经在单元格数组中,则可以使用{:}索引生成一个逗号分隔的列表,您可以将该列表传递给cat

C = {string('one'), string('two')};
result = cat(1, C{:})

附带说明一下,MATLAB中没有一维数组。 所有数组至少为二维(其中一个可以为1)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM