Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

New to MATLAB?

how to find match using strfind from 2 different dataset sizes

Asked by andrew

andrew

on 19 Sep 2013
Latest activity Commented on by Andrei Bobrov

Andrei Bobrov

on 26 Sep 2013
Accepted Answer by Andrei Bobrov

Andrei Bobrov

I currently have 2 sets of data and I am trying to find a match based off of it's name. For example in data1 it contains apple, banana, orange, pear,etc and data 2 contains apple-1, apple-2, apple-3, banana-1, banana-2, banana-3, orange, pear-1, pear-2, etc. Ultimately, I would like to write a script to match for example apple with apple-1, apple-2, apple-3 etc and print out the corresponding data:

Col 1      column 2
apple	apple-1
apple	apple-2
apple	apple-3
banana	banana-1
banana	banana-2
banana	banana-3
orange	orange
pear 	pear-1
pear 	pear-2

I have the following code:

[nRows,~]=size (data2); % 6204x13 matrix
[nRows2,]=size (id); %258x1 matrix
 temp2=data2.SAMPLEID;
 temp3=id;
for i=2:nRows 
    temp4=strfind (id,data2.SAMPLEID{i});
    ind= find(~isempty(temp4),1,'first');
for n=1:nRows2
   sampleid{n,1}=temp3;
   if ind ==1
     %  if ~isempty(ind{n});
   sampleid{n,2}=temp2;
   else  
  sampleid{n,2}=temp3;
 end
end
temp3= id{i};
temp2=data2.SAMPLEID{i};
end

1 Comment

Azzi Abdelmalek

Azzi Abdelmalek

on 19 Sep 2013

This is not clear

andrew

andrew

Products

No products are associated with this question.

2 Answers

Answer by Andrei Bobrov

Andrei Bobrov

on 20 Sep 2013
Edited by Andrei Bobrov

Andrei Bobrov

on 20 Sep 2013
Accepted answer
d1 = {'apple'; 'orange';'banana'; 'pear'}
d2 = {'apple-1'; 'pear-1'; 'apple-2';'banana-3'; 'orange'; 'apple-3'; 'banana-1';'banana-2'; 'pear-2'}
[v,iii] = sort(d2);
p = strcat(d1(1:end-1),{'|'});
p = strcat([p{:}],d1{end});
pp = regexp(d2,p,'match');
pp = cat(1,pp{:});
[~,ij] = ismember(pp,d1);
dd1 = d1(ij);
out = [dd1(iii),v]

2 Comments

andrew

andrew

on 23 Sep 2013

great this works I have another question? along with that i have d3 ={'red','green','yellow',orange'} how do i get it to match it with d1 and d2?

Andrei Bobrov

Andrei Bobrov

on 26 Sep 2013
d3 ={'red';'orange';'yellow';'green'}
d33=d3(ij)
color1 = d33(iii)
Andrei Bobrov

Andrei Bobrov

Answer by Jan Simon

Jan Simon

on 19 Sep 2013

"Col 1", "column 2", "data1", "data 2", "data2", "id" - I've lost the overview.

nRows concerns the size of the array data2. But the loop for i=1:nRows affects data2.SAMPLEID{i}. Perhaps you want data2(i).SAMPLEID or data2{i}.SampleID.

You have explained ropughly, what you want to achieve and posted some code. It is hard to recognize the relation between these two information. What is the actual question? Does the code run? Do you get any errors or unwanted results?

2 Comments

andrew

andrew

on 19 Sep 2013

i am getting an error message that says index exceeds matrix

Jan Simon

Jan Simon

on 20 Sep 2013

Please post the line of code and a copy of the complete error message. There is no chance that we can guess the reason of the problem.

Jan Simon

Jan Simon

Contact us