How to add a first row and a first column of zeros in a matrix?
34 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
P_M_Cap
am 14 Feb. 2022
Bearbeitet: Simon Dengler
am 14 Feb. 2022
hi,
i have a matrix, for example
A=[1 2 3; 4 5 6; 7 8 9]
how can i add a first row of zeros and a first colum of zeros to reach the following results?
B=[0 0 0 0; 0 1 2 3; 0 4 5 6; 0 7 8 9]
and how can i delete the last row and the last column of a matrix to reach this second results?
C=[0 0 0; 0 1 2; 0 4 5]
thank you very much matlab users!
0 Kommentare
Akzeptierte Antwort
Arif Hoq
am 14 Feb. 2022
A=[1 2 3; 4 5 6; 7 8 9];
AA=[zeros(1,3);A];
AAA=zeros(4,1);
B=[AAA AA];
B(:,end)=[];
B(end,:)=[]
0 Kommentare
Weitere Antworten (2)
Star Strider
am 14 Feb. 2022
A=[1 2 3; 4 5 6; 7 8 9];
B = zeros(size(A,1)+1,size(A,2)+1);
B(2:end,2:end) = A
C = B(1:end-1,1:end-1)
Four lines of code to do everything!
.
0 Kommentare
Simon Dengler
am 14 Feb. 2022
Bearbeitet: Simon Dengler
am 14 Feb. 2022
bit more general and faster?
A=[1 2 3; 4 5 6; 7 8 9];
B=zeros(size(A)+1);
B(2:end,2:end)=A
0 Kommentare
Siehe auch
Kategorien
Mehr zu Resizing and Reshaping Matrices finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!