How to select a specific column in matrices?
조회 수: 63 (최근 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!