MATLAB Answers

how to Concatenate big string matrices of different sizes

3 views (last 30 days)
amirhosein setoudeh
amirhosein setoudeh on 18 Jul 2021
Answered: Star Strider on 20 Jul 2021
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
Jan on 20 Jul 2021
How can the matrices have "one same column", if all have different sizes?

Sign in to comment.

Answers (2)

Jan on 20 Jul 2021
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);
This would be easier with tables objects and outerjoin().

Star Strider
Star Strider on 20 Jul 2021
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');
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.

Community Treasure Hunt

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

Start Hunting!

Translated by