简体   繁体   English

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

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

I have a working piece of code in MATLAB我在 MATLAB 中有一段有效的代码

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

However, I want to convert this to octave as MATLAB is a licensed version and everyone can't get access to it.但是,我想将它转换为八度,因为 MATLAB 是一个许可版本,每个人都无法访问它。

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

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

I want each row of matrix 'bag' to be filled with uneven number of characters, please suggest how this can be done in octave.我希望矩阵“袋子”的每一行都填充不均匀数量的字符,请建议如何在八度音程中完成。 Thanks.谢谢。

Since MATLAB strings have not been implemented in Octave, you will need to use a cell array of char arrays. Fortunately, this is pretty simple.由于 MATLAB字符串尚未在 Octave 中实现,您将需要使用字符 arrays 的元胞数组。幸运的是,这非常简单。 Just change the first two lines in your code sample to create a cell array of the proper size:只需更改代码示例中的前两行即可创建大小合适的元胞数组:

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

The result is:结果是:

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

The one annoyance is that in order to reference your char arrays in the cell array, you need to use curly braces instead of parentheses:一个烦恼是,为了在元胞数组中引用您的 char arrays,您需要使用大括号而不是圆括号:

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

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

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