How to select a specific column in matrices?
67 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Star Strider
on 7 Jun 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 Comments
Star Strider
on 7 Jun 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'
.
More Answers (1)
Monika Jaskolka
on 7 Jun 2021
Edited: Monika Jaskolka
on 7 Jun 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 Comments
See Also
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!