how to Concatenate big string matrices of different sizes

조회 수: 2 (최근 30일)
amirhosein setoudeh
amirhosein setoudeh 2021년 7월 18일
답변: Star Strider 2021년 7월 20일
hello everyone, i have 4 big string matrices with different sizes(a=192320x17,o=221847x8,r=55768x25 and v=394402x17) and they have one same column(case number) i want to know how can i Concatenate or merge them together to get a matice(c=394402x64) by putting rows of a specific case number from each matric to one row in c matric and put NAN or ' ' for rows that dont have any matches
i tried for loop for this but it takes a long time about 2days!!
i want to know if anyone have a faster way for this
thank you
  댓글 수: 2
Yazan
Yazan 2021년 7월 20일
Upload part of your data so that we can take a look.
Jan
Jan 2021년 7월 20일
How can the matrices have "one same column", if all have different sizes?

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

답변 (2개)

Jan
Jan 2021년 7월 20일
With some bold guessing:
% [UNTESTED] Please povide a small test data set
keyIndex = 1; % Guessing, that the "case number" is found in first column
keyList = union(union(union(a(:, keyIndex), o(:, keyIndex)), r(:, keyIndex)), v(:, keyIndex));
data = {a, o, r, v};
result = keyList;
for k = 1:4
aData = data{k};
[match, index] = ismember(keyList, aData(:, keyIndex));
result(match, end+1 : end+size(aData, 2) - 1) = aData(index, 2:end);
end
This would be easier with tables objects and outerjoin().

Star Strider
Star Strider 2021년 7월 20일
I would read all of them in as table arrays using readtable, then use the innerjoin function to join the first two tables, then the other tables in a loop to the first innerjoin operation. Use the case number variable as the Keys variable.
The procedure would go something lilke this:
T{1} = readtable('FirstMatrix.txt');
...
T{4} = readtable('FourthMatrix.txt');
TJ = innerjoin(T{1},T{2},'Keys','CaseNr');
for k = 1:2
TJ = innerjoin(TJ,T{k+2},'Keys','CaseNr');
end
This is UNTESTED since the matrices are not provided (and I cannot be certain that this procedure would work with your matrices, in any event). Make appropriate changes to get the result you want. It will likely be necessary to experiment.
.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by