필터 지우기
필터 지우기

Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

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

조회 수: 1 (최근 30일)
Anouk Heuvelmans
Anouk Heuvelmans 2018년 3월 28일
마감: MATLAB Answer Bot 2021년 8월 20일
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