Did you read doc on strncmp?
Info
此问题已关闭。 请重新打开它进行编辑或回答。
How can I compare cells of 2 matrices to see if part of the string is the same?
2 次查看(过去 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?
回答(1 个)
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 个评论
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!