Home > Blockchain >  construct a matrix with empty string in octave and later fill it with uneven number of characters in
construct a matrix with empty string in octave and later fill it with uneven number of characters in

Time:04-12

I have a working piece of code in 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.

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)

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.

CodePudding user response:

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. 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:

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