How to compare 5 cell arrays and get their contents in one cell array?

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:
Iwanted_output.png

3 commentaires

KSSV
KSSV le 14 Déc 2018
Modifié(e) : KSSV le 14 Déc 2018
strfind or strcmp or contains will work out.
@KSSV Thanks for your suggestions, My problem not in which command i can use but after alocate a cell of 600 rows and 5 cells , how i can place those columns altogether. foe evry row?
Anyone else can help me plz?

Connectez-vous pour commenter.

 Réponse acceptée

Stephen23
Stephen23 le 14 Déc 2018
Modifié(e) : Stephen23 le 14 Déc 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 commentaires

@Stephen Cobeldick my output should have 5 columns not only 4 as i mentioned in my post and when there is no overlap rows should show 0 and an ampty cell [ ]
I tried your code and it works well but still didn't get my wanted_output( 600rows*5 columns)
could you plz fix that and sorry for my low efficiency , i'm still learning matlab...
Thanks a lot Stephen
Put this after the loop:
CZ = [C0(:),CZ]
@Stephen Cobeldick Yes i did it to merge all the columns and what about repacing [] by 0
"...what about repacing [] by 0"
CZ(cellfun('isempty',CZ)) = {0}
Chocho, you are not are not a beginner anymore. You know how to use indexing. You know about cell arrays. You are perfectly capable of joining two cells arrays together yourself, or finding empty cells and replacing them. Experiment, try things, make mistakes and learn...
chocho
chocho le 14 Déc 2018
Modifié(e) : chocho le 14 Déc 2018
@Stephen Cobeldick Yes i totally agree with you, You know i was thinking that you will do these 2 things inside your code inside the for loop but once you got the output y did it thats'why i was confusing otherwis i can do as i already did before.
Thanks Stephen a lot , i learned alot today.
Jan
Jan le 14 Déc 2018
Modifié(e) : Jan le 14 Déc 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
"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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by