How to compare 5 cell arrays and get their contents in one cell array?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Hello gyus,
I have a cell array of size 600*1 and 4 cell arrays: cell1(361*2), cell2(376*2), cell3(362*2) and cell4(351*2).
cell array(600*1) have overlap rows with all of these 4 cell arrays column1 rows;
I want to output a cell of size 600*4 with the 600 rows and 4 columns, which are the contents of the 4 cells column 2 contents if there is an overlap with their rows , if not put 0.
More specifically some rows in cell array( 600*1) may exist in all the rows of all the 4 cells or may exist in just some of them.
Inputs: cell array, cell1, cell2, cell3, and cell4
I appreciate any help plz!
Original cell:
'R-HSA-3000171:R-HSA-3000171'
'R-HSA-3000178:R-HSA-3000178'
'R-HSA-3000471:R-HSA-3000471'
'R-HSA-3000480:R-HSA-3000480'
'R-HSA-3000484:R-HSA-3000484'
cell1
'R-HSA-3000171:R-HSA-3000171' 'PDGFB'
'R-HSA-5419276:R-HSA-5419276' ' MRPS30,MRPL48,MRPS33'
cell2
'R-HSA-3000171:R-HSA-3000171' 'TTR'
'R-HSA-5655302:R-HSA-5655302' 'FGF1,FGF17'
cell3
'R-HSA-3000171:R-HSA-3000171' 'COL4A6,LAMA1,COL4A5'
'R-HSA-416482:R-HSA-416482' 'GNB2,ARHGEF16,ABR'
cell4
'R-HSA-3000171:R-HSA-3000171' 'COL11A2,COL3A1'
'R-HSA-5419276:R-HSA-5419276' 'MRPL1'
Iwanted_output:
3 Kommentare
Akzeptierte Antwort
Stephen23
am 14 Dez. 2018
Bearbeitet: Stephen23
am 14 Dez. 2018
Use ismember in a loop:
C0 = {'A';'B';'C';'D';'F'}; % your 600x1 cell array.
C1 = {'A',1;'C',123;'X',0};
C2 = {'B',21;'D',2222};
C3 = {'C',321;'F',333333;'A',111111};
C4 = {'C',444};
CN = {C1,C2,C3,C4}; % all inputs in one cell array.
CZ = cell(numel(C0),numel(CN)); % preallocate output array.
for k = 1:numel(CN)
[idx,id0] = ismember(CN{k}(:,1),C0);
CZ(id0(idx),k) = CN{k}(idx,2);
end
7 Kommentare
Jan
am 14 Dez. 2018
Bearbeitet: Jan
am 14 Dez. 2018
@chocho: Your question was: "I want to output a cell of size 600*4 with the 600 rows and 4 columns". This sounds clearly as 4 columns, not 5.
So you want to add C0 as first column? Easy:
CZ = cat(2, C0, CZ)
Or modify the code:
CZ = cell(numel(C0), numel(CN) + 1); % preallocate output array.
CZ(:) = {0}; % Or {'0'} or {'[]'} or whatever you want
CZ(:,1) = C0;
for k = 1:numel(CN)
[idx, id0] = ismember(CN{k}(:,1), C0);
CZ(id0(idx), k + 1) = CN{k}(idx, 2);
end
Stephen23
am 14 Dez. 2018
"i was thinking that you will do these 2 things inside your code inside the for loop "
Yes, you could also do that, that is a good idea. It might even be more efficient and/or simpler to do those steps inside the loop.... you should give it a try.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!