How to insert an element into matrix?

98 visualizaciones (últimos 30 días)
George Ansari
George Ansari el 22 de Ag. de 2017
Comentada: Image Analyst el 14 de Oct. de 2020
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.

Respuestas (2)

Image Analyst
Image Analyst el 22 de Ag. de 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, :)]
  4 comentarios
Amay Gupta
Amay Gupta el 14 de Oct. de 2020
can the first one be used in loops?
if yes please explain.
Image Analyst
Image Analyst el 14 de Oct. de 2020
What is "the first one"? Please explain. Why do you want loops?

Iniciar sesión para comentar.


José-Luis
José-Luis el 22 de Ag. de 2017
Editada: José-Luis el 22 de Ag. de 2017
result = [A;B]
Or use cat()

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by