# Append rows at the end of Matrix

2,571 次查看（过去 30 天）
Trushit2014-1-27

Hi,
a = [1 2 3 ; 4 5 6; 7 8 9]; --> 3x3 matrix
I want to insert at the end number of raws with same elements such as [5 5 5] and make the matrix 10 x 3 i.e. I want to insert 7 more raws with [5 5 5]. Please explain.

### 采纳的回答

Azzi Abdelmalek 2014-1-27

a = [1 2 3 ; 4 5 6; 7 8 9];
b=[5 5 5]
##### 5 个评论显示 4更早的评论隐藏 4更早的评论
user924 2021-1-26

Perhaps try creating a 5x5 matrix of zeros and using a for loop to overwrite the elements that you want to be non-zero.
a =
0 0 0 0 0
0 0 0 0 0
0 0 1 2 3
0 0 4 5 6
0 0 7 8 9
a = zeros(5);
b = [1:9];
width = 3;
[m,n] = size(a);
for row = m:-1:1
for col = n:-1:n-2
if size(b) > 0
a(row, col) = b(end);
b = b(1:end-1);
end
end
end
a

### 更多回答（2 个）

Amit 2014-1-27
A = [1 2 3;4 5 6;7 8 9];
A = [A; ones(7,3)*5];
##### 1 个评论显示 无隐藏 无
Trushit 2014-1-30
Thank you. It's short and speedy.

Michael Hawks 2019-5-2
Another method:
a = [1 2 3 ; 4 5 6; 7 8 9];
b=[5 5 5];
a( end+1, : ) = b;
or
a( :, end+1 ) = b';

### 类别

Find more on Creating and Concatenating 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