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

New to MATLAB?

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

Asked by Donghui Sun

Donghui Sun (view profile)

on 20 Sep 2013
Accepted Answer by Simon

Simon (view profile)

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

Donghui Sun (view profile)

Products

No products are associated with this question.

2 Answers

Answer by Simon

Simon (view profile)

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

0 Comments

Simon

Simon (view profile)

Answer by Andrei Bobrov

Andrei Bobrov (view profile)

on 20 Sep 2013
Edited by Andrei Bobrov

Andrei Bobrov (view profile)

on 20 Sep 2013

if you have Image Processing Toolbox

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

0 Comments

Andrei Bobrov

Andrei Bobrov (view profile)

Contact us