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

Thread Subject:
Finding Identical Elements in Different Structures

Subject: Finding Identical Elements in Different Structures

From: Kevin Ellis

Date: 3 Nov, 2012 05:38:13

Message: 1 of 4

Hello,

I have a unique problem that can't be solved using conventional methods (I have tried numerous times). I have two structure arrays, one is <1x13688> and the other is <1x20497>, in terms of size. Within each structure there are about 20 different fields. All I want to do is find a way to compare the two structures on one fieldname (in my case, documentnumber). I have tried using a "for" loops, but they make the program run very slow because you have 13688 x 20497 instances. I have tried bringing both structures into a cell array and removing all elements except document number and then using "setdiff", "isequal", or "ismember" to find identical elements (documentnumbers) and output their index numbers, but I am wondering if there is a more direct method using the structures, possibly "structfun?"

I found that the method I was trying was not working correctly as many documentnumbers were being missed (possibly as a result of comparing strings). So, as an example, I have the following two cell arrays which came from the two separate structure arrays after using "reshape" and removing unnecessary elements:

DESCCell =
    '351211402007FF00'
    '351211402012SE00'
    '351211402014FF00'
    '351211402027SE00'
    '351211402038FF00'

SASCell =
    '351112104FDIE002'
    '351112104GNLS002'
    '351115245266SE00'
    '351115248272SE00'
    '351211402007FF00'

Clearly, in this case, there is only on equal element between the two cell arrays (the first of the DESCCell array and the last of the SASCell array) and I want to find the index of that element from the DESCCell array and the index of that element of the SASCell array which correspond to each other. Of course, I want to eventually do this over a much larger data set. Nested "for" loops produce inaccurate results and slow the program to a halt and the way I have been using "ismember" or "setdiff" seem to not be working so any help would be much appreciated. Thanks so much.

Kevin Ellis

Subject: Finding Identical Elements in Different Structures

From: Steven_Lord

Date: 5 Nov, 2012 15:43:19

Message: 2 of 4



"Kevin Ellis" <kevin.ellis86@gmail.com> wrote in message
news:k72ak5$jsd$1@newscl01ah.mathworks.com...
> Hello,
>
> I have a unique problem that can't be solved using conventional methods (I
> have tried numerous times). I have two structure arrays, one is <1x13688>
> and the other is <1x20497>, in terms of size. Within each structure there
> are about 20 different fields. All I want to do is find a way to compare
> the two structures on one fieldname (in my case, documentnumber). I have
> tried using a "for" loops, but they make the program run very slow because
> you have 13688 x 20497 instances. I have tried bringing both structures
> into a cell array and removing all elements except document number and
> then using "setdiff", "isequal", or "ismember" to find identical elements
> (documentnumbers) and output their index numbers, but I am wondering if
> there is a more direct method using the structures, possibly "structfun?"

If I understand what you're trying to do, ISMEMBER would be the tool I'd
use.

> I found that the method I was trying was not working correctly as many
> documentnumbers were being missed (possibly as a result of comparing
> strings). So, as an example, I have the following two cell arrays which
> came from the two separate structure arrays after using "reshape" and
> removing unnecessary elements:
>
> DESCCell = '351211402007FF00'
> '351211402012SE00'
> '351211402014FF00'
> '351211402027SE00'
> '351211402038FF00'
>
> SASCell = '351112104FDIE002'
> '351112104GNLS002'
> '351115245266SE00'
> '351115248272SE00'
> '351211402007FF00'
>
> Clearly, in this case, there is only on equal element between the two cell
> arrays (the first of the DESCCell array and the last of the SASCell array)
> and I want to find the index of that element from the DESCCell array and
> the index of that element of the SASCell array which correspond to each
> other. Of course, I want to eventually do this over a much larger data
> set. Nested "for" loops produce inaccurate results and slow the program to
> a halt and the way I have been using "ismember" or "setdiff" seem to not
> be working so any help would be much appreciated. Thanks so much.

When I used ISMEMBER with your data above, it gave me the results I
expected:

>> [common, ia, ib] = intersect(DESCCell, SASCell)
common =
    '351211402007FF00'

ia =
     1

ib =
     5

That string is in fact the first element of DESCCell and the fifth element
of SASCell. There are no other common strings between the two. If you're
seeing something different, please show a SMALL sample of your code that
displays something other than what you expect.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Finding Identical Elements in Different Structures

From: Ben

Date: 5 Nov, 2012 22:01:08

Message: 3 of 4

I've found this file on the file exchange helpful before...


http://www.mathworks.com/matlabcentral/fileexchange/22752-compare-structures

Subject: Finding Identical Elements in Different Structures

From: Yair Altman

Date: 7 Nov, 2012 07:13:08

Message: 4 of 4

"Kevin Ellis" wrote in message <k72ak5$jsd$1@newscl01ah.mathworks.com>...
> Hello,
>
> I have a unique problem that can't be solved using conventional methods (I have tried numerous times). I have two structure arrays, one is <1x13688> and the other is <1x20497>, in terms of size. Within each structure there are about 20 different fields. All I want to do is find a way to compare the two structures on one fieldname (in my case, documentnumber). I have tried using a "for" loops, but they make the program run very slow because you have 13688 x 20497 instances. I have tried bringing both structures into a cell array and removing all elements except document number and then using "setdiff", "isequal", or "ismember" to find identical elements (documentnumbers) and output their index numbers, but I am wondering if there is a more direct method using the structures, possibly "structfun?"
>
> I found that the method I was trying was not working correctly as many documentnumbers were being missed (possibly as a result of comparing strings). So, as an example, I have the following two cell arrays which came from the two separate structure arrays after using "reshape" and removing unnecessary elements:
>
> DESCCell =
> '351211402007FF00'
> '351211402012SE00'
> '351211402014FF00'
> '351211402027SE00'
> '351211402038FF00'
>
> SASCell =
> '351112104FDIE002'
> '351112104GNLS002'
> '351115245266SE00'
> '351115248272SE00'
> '351211402007FF00'
>
> Clearly, in this case, there is only on equal element between the two cell arrays (the first of the DESCCell array and the last of the SASCell array) and I want to find the index of that element from the DESCCell array and the index of that element of the SASCell array which correspond to each other. Of course, I want to eventually do this over a much larger data set. Nested "for" loops produce inaccurate results and slow the program to a halt and the way I have been using "ismember" or "setdiff" seem to not be working so any help would be much appreciated. Thanks so much.
>
> Kevin Ellis


Try ObjDiff - a generic object comparison utility on the File Exchange:
http://www.mathworks.com/matlabcentral/fileexchange/14395-objdiff-generic-object-comparator

Yair Altman
http://UndocumentedMatlab.com
 

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us