Info

此问题已关闭。 请重新打开它进行编辑或回答。

How can I compare cells of 2 matrices to see if part of the string is the same?

1 次查看(过去 30 天)
I have 2 datasets for my subjects and due to blinding of the documents the order of the subjects in the 2 matrices I have does not match. Example:
cell array 1:
WTCdatanames_sham = {'WTC18_2_2_P16_whis_sham_rec' 'WTC18_2_1_P16_whis_sham_rec' 'WTC18_2_3_P16_whis_sham_rec'}
cell array 2:
WTCdatanames_stim = {'WTC18_2_2_P16_whis_stim_rec' 'WTC18_2_3_P16_whis_stim_rec' 'WTC18_2_1_P16_whis_stim_rec'}
Say, for every column in cell array 1, I want to use the first part (WTC18_2_2) to find the corresponding column in cell array 2. How would I be able to do this? Can I in some way write a loop in which it compares the first 10 letters from a cell in cell array 1 to all cells in cell array 2?
  2 个评论
Anouk Heuvelmans
Anouk Heuvelmans 2018-3-28
I did, I tried the following: s1 = WTCshamrecnames(1,1); s2 = WTCstimrecnames(1,1); tf = strncmp(s1,s2,9) This should give me 1 because WTC18_2_2 is for both cell arrays in the first column but my output of tf is 0

回答(1 个)

Birdman
Birdman 2018-3-28
This should do it:
loc='WTC18_2_2';
strncmp(loc,WTCdatanames_stim,numel(loc))
and this will return logical
1 0 0
which means loc is located in the first element of WTCdatanames_stim, not others.
  2 个评论
Anouk Heuvelmans
Anouk Heuvelmans 2018-3-28
Thanks for your help. However, this would only give me the location of the first one. instead of saying 'loc='WTC18_2_2'; I would like to have the program extracting that part for every column because my actual dataset is a lot bigger and I don't want to write out loc = ... for every single subject. Do you know of a way in which I can ask matlab to read out this first part from my cell? (I hope you understand my question)
Birdman
Birdman 2018-3-28

Maybe this could be helpful:

 n=9; %corresponds to WTC_2_2, WTC_2_1 and WTC_2_3 respectively 
 for i=1:size(WTCdatanames_sham,2)
    idx(i,:)=strncmp(WTCdatanames_sham{i}(1:n),WTCdatanames_stim,n);
 end

此问题已关闭。

Community Treasure Hunt

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

Start Hunting!

Translated by