How to insert an element into matrix?

233 ビュー (過去 30 日間)
George Ansari
George Ansari 2017 年 8 月 22 日
コメント済み: Image Analyst 2020 年 10 月 14 日
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.

回答 (2 件)

Image Analyst
Image Analyst 2017 年 8 月 22 日
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 件のコメント
Image Analyst
Image Analyst 2020 年 10 月 14 日
What is "the first one"? Please explain. Why do you want loops?


José-Luis 2017 年 8 月 22 日
編集済み: José-Luis 2017 年 8 月 22 日
result = [A;B]
Or use cat()


Find more on Resizing and Reshaping Matrices in Help Center and File Exchange



Community Treasure Hunt

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

Start Hunting!

Translated by