Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

What should I do to add a new row and a new column to a matrix?

Asked by Donghui Sun on 20 Sep 2013

Just ad the title shows, I have a matrix, for example M with the size 3-by-4. The M is [1 2 3 4;5 6 7 8;1 2 3 4]

What should I do if i want to get another matrix ,N like that: [0 0 0 0 0 0; 0 1 2 3 4 0; 0 5 6 7 8 0; 0 1 2 3 4 0; 0 0 0 0 0 0];

0 Comments

Donghui  Sun

Products

No products are associated with this question.

2 Answers

Answer by Simon on 20 Sep 2013
Accepted answer
N = zeros(size(M)+2);
N(2:end-1, 2:end-1) = M;

0 Comments

Simon
Answer by Andrei Bobrov on 20 Sep 2013
Edited by Andrei Bobrov on 20 Sep 2013

if you have Image Processing Toolbox

N = padarray(M,[1 1]);

0 Comments

Andrei Bobrov

Contact us