Filter löschen
Filter löschen

Add value at new position automatically to matrix

3 Ansichten (letzte 30 Tage)
Fabian Haslwanter
Fabian Haslwanter am 3 Jan. 2023
Bearbeitet: Stephen23 am 3 Jan. 2023
Hello there, following problem:
I have a matrix A. For an algorithm a value should be added in the last place of a new row and column, rest filled with 0. I tried this in B but the result should look like in C. Is there an easy way to do this without manually adding an additional column and row?
Thank you very much!
A = [1 2 3;4 5 6]
A = 2×3
1 2 3 4 5 6
B = {A 0;0 1}
B = 2×2 cell array
{2×3 double} {[0]} {[ 0]} {[1]}
C = [1 2 3 0;4 5 6 0;0 0 0 1]
C = 3×4
1 2 3 0 4 5 6 0 0 0 0 1
  3 Kommentare
Torsten
Torsten am 3 Jan. 2023
Yes, it's fine.
Or
A = [1 2 3;4 5 6];
value = 1.0;
A = [A,zeros(size(A,1),1);zeros(1,size(A,2)),value]
A = 3×4
1 2 3 0 4 5 6 0 0 0 0 1
Stephen23
Stephen23 am 3 Jan. 2023
Bearbeitet: Stephen23 am 3 Jan. 2023
"This is my current solution:"
Just use BLKDIAG or basic MATLAB indexing:
A = [1,2,3;4,5,6];
A(end+1,end+1) = 1
A = 3×4
1 2 3 0 4 5 6 0 0 0 0 1

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Matt J
Matt J am 3 Jan. 2023
Bearbeitet: Matt J am 3 Jan. 2023
A = [1 2 3;4 5 6];
C=[A, zeros(2,1);
zeros(1,3) , 1 ]
C = 3×4
1 2 3 0 4 5 6 0 0 0 0 1
or
C=blkdiag(A,1)
C = 3×4
1 2 3 0 4 5 6 0 0 0 0 1

Weitere Antworten (0)

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by