Append rows at the end of Matrix

조회 수: 1,718 (최근 30일)
Trushit
Trushit 2014년 1월 27일
편집: user924 2021년 1월 26일
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
Azzi Abdelmalek 2014년 1월 27일
편집: Azzi Abdelmalek 2014년 1월 27일
a = [1 2 3 ; 4 5 6; 7 8 9];
b=[5 5 5]
c=[a;b]% add one row
c=[a;repmat(b,7,1)] %add 7rows
  댓글 수: 5
jerrell lim
jerrell lim 2021년 1월 20일
is there a way to create a matlab matrix 5 by 5 in one command without typing each number individually ?
the numbers are [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]
user924
user924 2021년 1월 26일
편집: 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
Amit 2014년 1월 27일
A = [1 2 3;4 5 6;7 8 9];
A = [A; ones(7,3)*5];
  댓글 수: 1
Trushit
Trushit 2014년 1월 30일
Thank you. It's short and speedy.

댓글을 달려면 로그인하십시오.


Michael Hawks
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';

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by