Replace Row in a Matrix with an Vector

조회 수: 56 (최근 30일)
Haocheng Du
Haocheng Du 2019년 9월 18일
답변: Daniel Neubauer 2022년 11월 2일
I have generated a matrix M with M = magic(5), and I was asked to creat a new matrix M_new, where M_new is M with its last row replaced by a row vector. I was required to use only one line of code, so no equal signs...
Original question:
M_new = %Matrix M, but the last row is replaced by vector Row
% Hint: This can be done in one line of code

답변 (2개)

KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 9월 18일
편집: KALYAN ACHARJYA 2019년 9월 18일
Replace Row in a Matrix with an Vector
matrix(n,:)=vector_data
n is row number, Ensured that vector_data length and row length are same

Daniel Neubauer
Daniel Neubauer 2022년 11월 2일
M=magic(5)
M = 5×5
17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9
r=[0 0 0 0 0]
r = 1×5
0 0 0 0 0
M_new=[M(1:end-1,:);r]
M_new = 5×5
17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 0 0 0 0 0

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by