MATLAB Answers

How to insert an element into matrix?

Asked by George Ansari

George Ansari (view profile)

on 22 Aug 2017
Latest activity Answered by Image Analyst

Image Analyst (view profile)

on 22 Aug 2017
Suppose I have a matrix A = ones(4,6). I want to combine it with a vector B = zeros(1,6) to make another matrix C of size 5 by 6, where the first four rows would be ones and fifth would be zero.

0 Comments

Sign in to comment.

3 Answers

Image Analyst (view profile)

Answer by Image Analyst

Image Analyst (view profile)

on 22 Aug 2017

Here's code to do both inserting (like your subject line) and appending (like your message body):
A = ones(4,6)
B = zeros(1,6)
% To append / concatenate:
C = [A; B]
% To insert into a specified row number:
rowToInsert = 3 % Whatever you want.
C = [A(1:rowToInsert-1, :); B; A(rowToInsert:end, :)]

0 Comments

Sign in to comment.

Stephen Cobeldick (view profile)

Answer by Stephen Cobeldick

Stephen Cobeldick (view profile)

on 22 Aug 2017

C = [A;B]

0 Comments

Sign in to comment.

José-Luis (view profile)

Answer by José-Luis

José-Luis (view profile)

on 22 Aug 2017
Edited by José-Luis

José-Luis (view profile)

on 22 Aug 2017

result = [A;B]
Or use cat()

0 Comments

Sign in to comment.