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

octave

Solution

An easy inline way to do this is:

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

Problem

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?

Original source