how to Concatenate big string matrices of different sizes

2 visualizaciones (últimos 30 días)
amirhosein setoudeh
amirhosein setoudeh el 18 de Jul. de 2021
Respondida: Star Strider el 20 de Jul. de 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
  2 comentarios
Yazan
Yazan el 20 de Jul. de 2021
Upload part of your data so that we can take a look.
Jan
Jan el 20 de Jul. de 2021
How can the matrices have "one same column", if all have different sizes?

Iniciar sesión para comentar.

Respuestas (2)

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

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

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by