How to insert a column/row of ones into a matrix?
One way to do it is:
function [result] = prependOnes(matrix)
result = [ones(rows(matrix),1) matrix];
end
prependOnes(b)
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
An easy inline way to do this is:
b = [ones(size(b, 1), 1) b];