How to insert a column/row of ones into a matrix?

Octave

Octave Problem Overview


Suppose that we have a 3x3 matrix like

b = 2 * eye(3);
ans =

2   0   0
0   2   0
0   0   2

and I want to a 3x4 matrix like

1   2   0   0
1   0   2   0
1   0   0   2

What is the best way to get it?

Octave Solutions


Solution 1 - Octave

An easy inline way to do this is:

b = [ones(size(b, 1), 1) b];

Solution 2 - Octave

In GNU Octave:

Concatenate two matrices together horizontally: separate the matrices by a Space,
and enclose the result in square brackets:

X = [ones(rows(X), 1) X];

Simpler examples:
Glue on one number (add a horizontal "Column" element with a Comma):

octave:1> [[3,4,5],8]
ans =
   3   4   5   8

Glue on another matrix horizontally (as additional Columns) by using a Comma:

octave:2> [[3,4,5],[7,8,9]]
ans = 
   3   4   5   7   8   9

Glue on another matrix vertically (as additional Rows) by using a Semicolon:

octave:3> [[3,4,5];[7,8,9]]
ans =
   3   4   5
   7   8   9

Solution 3 - Octave

One way to do it is:

function [result] = prependOnes(matrix)
  result = [ones(rows(matrix),1) matrix];
end

prependOnes(b)

Solution 4 - Octave

I Would like to point out that [A B] actually creates a new matrix as oppose to inserting, which should be obvious from its syntactic construction. But I see a lot of place use the word append or insert that I wonder whether octave does any clever thing behind, so I run a test

function testAppend(repeat)

profile off;
profile on; 
testAdd(repeat);
testAssign(repeat);
profile off;
data = profile('info');
profshow(data, 5); 

end

function testAdd(repeat)
for i = 1:repeat
    A = ones(100, 1); 
    B = ones(100, 1); 
    for j = 1:10000
        A = [A B]; 
    end 
end
end

function testAssign(repeat)
for i = 1:repeat
    B = ones(100, 1); 
    A = zeros(100, 10000);
    for j = 2:10001
        A(:, j) = B;
    end 
end
end

and here is the result

octave:1> testAppend(1)
   #              Function Attr     Time (s)        Calls
---------------------------------------------------------
   1    testAppend>testAdd             9.454            1
   3 testAppend>testAssign             0.023            1
   4                 zeros             0.001            1
   5               profile             0.000            1
   2                  ones             0.000            3

So if you need to continuously grow your matrix, create the target matrix first then assign value to it. Or implement something like resizing-matrix if you don't know the ultimate dimension in advance.

Solution 5 - Octave

The function padarray (from the image package) was designed to do exactly that.

octave> b = 2 * eye (3)
b =

Diagonal Matrix

   2   0   0
   0   2   0
   0   0   2

octave> padarray (b, [0 1], 1, "pre")
ans =

   1   2   0   0
   1   0   2   0
   1   0   0   2

The functions reads pre pad the variable b with 0 rows and 1 column. The function allows for a big flexibility when padding matrices but may be overkill for really simple stuff.

Solution 6 - Octave

The b matrix is defined as below

>> b = [1 2 3;4 5 6;7 8 9]
b =

   1   2   3
   4   5   6
   7   8   9

creating a new matrix by adding a new column of 1s as below

>> [ones(size(b), 1) b]
ans =

   1   1   2   3
   1   4   5   6
   1   7   8   9

Creating a new matrix by adding a new row of 1s as below

>> [ones(size(b)(2), 1)';b]
ans =

   1   1   1
   1   2   3
   4   5   6
   7   8   9

Solution 7 - Octave

You have a 3x3 matrix like

b = 2 * eye(3)
ans =

2   0   0
0   2   0
0   0   2

You can add a column of ones as below and recieve new 4x3 matrix

[ones(size(b, 1), 1) b]
ans =

1   2   0   0
1   0   2   0
1   0   0   2

You can add a row of ones as below and recieve new 3x4 matrix

[ones(1,size(b, 2)); b]
ans =

1   1   1
2   0   0
0   2   0
0   0   2

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRubenLagunaView Question on Stackoverflow
Solution 1 - Octavecarl_hView Answer on Stackoverflow
Solution 2 - Octavesdn342323View Answer on Stackoverflow
Solution 3 - OctaveRubenLagunaView Answer on Stackoverflow
Solution 4 - OctaveTianwei ChenView Answer on Stackoverflow
Solution 5 - OctavecarandraugView Answer on Stackoverflow
Solution 6 - OctaverainuView Answer on Stackoverflow
Solution 7 - OctaveOleg UshakovView Answer on Stackoverflow