How to add a column between two columns?

12 Ansichten (letzte 30 Tage)
Jose Grimaldo
Jose Grimaldo am 5 Feb. 2020
Kommentiert: C'lette am 4 Feb. 2024
I have a matrix 3x3 and a column vector 3x1. I'm trying to add the column vector between the second and third column of the 3x3 matrix.How can i add the column to the matrix?
M1=randi([-10,20],[3,3]); % M1 is 3x3 matrix
r=[5;5;5]; % r is 3x1 vector

Akzeptierte Antwort

Adam Danz
Adam Danz am 5 Feb. 2020
Bearbeitet: Adam Danz am 5 Feb. 2020
out = [M1(:,1:2), r, M1(:,3)];
  1 Kommentar
C'lette
C'lette am 4 Feb. 2024
I suggest a little change, just to avoid hardcoding:
out = [M1(:,1:2), r, M1(:,3:end)];

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Data Types finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by