how do I merge multiple matrices into one?

조회 수: 8 (최근 30일)
Shirish Kulkarni
Shirish Kulkarni 2021년 2월 25일
답변: Walter Roberson 2021년 3월 2일
I have 50 1111 x 3 size matrics. I want to combine them into one final matrix such as all first rows from individual matrices are in first 50 rows and then all 2nd rows and so on. Also, for each set of rows it should insert a header row at the beginning.
How do I do this in Matlab?
  댓글 수: 5
Walter Roberson
Walter Roberson 2021년 2월 25일
Is this for writing into an xlsx file? SInce you cannot create a numeric array that has header rows in the middle of it, and if you were to create a table() object all of the entries would have to be cells in order to permit the header rows to be in middle of it.
Shirish Kulkarni
Shirish Kulkarni 2021년 2월 26일
I have the header rows (numerical values) in another seperate matrix that I want to insert in resultant matrix.

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2021년 3월 2일
One approach
N = 4; %50 in your original
Rows = 2; %1111 in your original
%generate some data for illustration
M = arrayfun(@(ignore) randi(9,Rows,3), (1:N).', 'uniform', 0)
M = 4x1 cell array
{2×3 double} {2×3 double} {2×3 double} {2×3 double}
cell2mat(M) %for illustration
ans = 8×3
1 3 7 1 6 7 6 1 8 4 9 5 9 8 3 4 6 9 4 8 8 3 7 6
nrow = size(M{1},1);
headers = repmat(-(1:nrow).', 1, 3);
grouped = arrayfun(@(row) cell2mat(cellfun(@(C) C(row,:), M, 'uniform', 0)), 1:nrow, 'uniform', 0)
grouped = 1x2 cell array
{4×3 double} {4×3 double}
headered = [num2cell(headers,2).'; grouped]
headered = 2x2 cell array
{1×3 double} {1×3 double} {4×3 double} {4×3 double}
joint = cell2mat(headered(:))
joint = 10×3
-1 -1 -1 1 3 7 6 1 8 9 8 3 4 8 8 -2 -2 -2 1 6 7 4 9 5 4 6 9 3 7 6

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by