How to select a specific column in matrices?
67 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Star Strider
2021-6-7
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 个评论
Star Strider
2021-6-7
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'
.
更多回答(1 个)
Monika Jaskolka
2021-6-7
编辑:Monika Jaskolka
2021-6-7
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!