构造一个八度为空字符串的矩阵,然后在每行中用奇数个字符填充它

construct a matrix with empty string in octave and later fill it with uneven number of characters in each row

我在 MATLAB 中有一段有效的代码

empty_string = "";
bag = repmat(empty_string, 4, 1);
bag(2) = "second row";
bag(3) = "third";

但是,我想把它转换成八度,因为 MATLAB 是一个授权版本,每个人都无法访问它。

empty_string = blanks(1);
bag = repmat(empty_string, 4, 1);
bag(2) = "second row";
bag(3) = "third";

这给出 error: =: nonconformant arguments (op1 is 1x1, op2 is 1x10)

我希望矩阵 'bag' 的每一行都填充奇数个字符,请建议如何在八度音阶中完成。谢谢

由于 MATLAB strings 尚未在 Octave 中实现,您将需要使用字符数组的元胞数组。幸运的是,这非常简单。只需更改代码示例中的前两行即可创建适当大小的元胞数组:

bag = cell(4,1);
bag(2) = "second row";
bag(3) = "third";

结果是:

bag =
{
  [1,1] = [](0x0)
  [2,1] = second row
  [3,1] = third
  [4,1] = [](0x0)
}

一个烦恼是,为了在元胞数组中引用您的字符数组,您需要使用大括号而不是圆括号:

>> second = bag{2}
second = second row