How to select a specific column in matrices?
67 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Ara Guz
el 7 de Jun. de 2021
Comentada: Star Strider
el 7 de Jun. de 2021
I have four (4x4) matrices A B C D. I need to put all the second colums of the four matrices in another matrix X. I tried using
xdatatemp = xdata(:,[end 2]); X = xdatatemp
but it shows an error. Thank you in advance!
0 comentarios
Respuesta aceptada
Star Strider
el 7 de Jun. de 2021
Concatenate them, then select the second column of the concatenated matrix —
A = randi(9,4)
B = randi(9,4)
C = randi(9,4)
D = randi(9,4)
ABCD = cat(3,A,B,C,D)
NewMatrix = squeeze(ABCD(:,2,:)) % Elimiinate Singleton Dimensions with 'squeeze'
.
3 comentarios
Star Strider
el 7 de Jun. de 2021
To get the second row simply requires changing the addressing slightly from:
NewMatrix = squeeze(ABCD(:,2,:)) % Elimiinate Singleton Dimensions with 'squeeze'
to:
NewMatrix = squeeze(ABCD(2,:,:)).' % Elimiinate Singleton Dimensions with 'squeeze'
Note the added transposition.
Running tthe code with that change:
A = randi(9,4)
B = randi(9,4)
C = randi(9,4)
D = randi(9,4)
ABCD = cat(3,A,B,C,D)
NewMatrix = squeeze(ABCD(2,:,:)).' % Elimiinate Singleton Dimensions with 'squeeze'
.
Más respuestas (1)
Monika Jaskolka
el 7 de Jun. de 2021
Editada: Monika Jaskolka
el 7 de Jun. de 2021
A = ones(4)
B = ones(4)*2
C = ones(4)*3
D = ones(4)*4
X = [A(:,2), B(:,2), C(:,2), D(:,2)]
0 comentarios
Ver también
Categorías
Más información sobre Matrices and Arrays en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!