Combining two Matrices every other row

조회 수: 42 (최근 30일)
Pranjal Bhatia
Pranjal Bhatia 2021년 7월 28일
답변: James Tursa 2021년 7월 28일
Lets Say I have two Matrices of 45X4 - A & B
I want to combine them in a New Matrix say C, where the first row is of matrix A and the second row is from B, similarly the third row is from A and the fourth row is from B and so on. I have tried various combos of converting A and B first to a coloumn matrix and then combining them but I always get a wrong answer. I'll write an example as well as to what I exactly Want
A =
1 2 3 4
5 6 7 8
B =
11 22 33 44
55 66 77 88
C =
1 2 3 4
11 22 33 44
5 6 7 8
55 66 77 88

답변 (1개)

James Tursa
James Tursa 2021년 7월 28일
You could do direct assignment. E.g.,
[m,n] = size(A);
C = zeros(2*m,n);
C(1:2:end,:) = A;
C(2:2:end,:) = B;

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by