How to insert an element into matrix?

조회 수: 148 (최근 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()


Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기



Community Treasure Hunt

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

Start Hunting!

Translated by