Can you operate over two structured arrays with the same field names and kind of content?

Hi everyone,
I have two structures, which contain the results of two independent models, each fit to several datasets. So:
model1
.set1 [100 x 1] double
.set2 [150 x 1] double
model2
.set1 [100 x 1] double
.set2 [150 x 1] double
I want to find the intersection of the vectors between the two models, as efficiently as possible. Of course I could loop, but there might be a smarter way. I was thinking something like structfun, but it has to work over both structures in lockstep. I could also transform the structures to cell arrays, if that makes the operation possible. What I want in the end is:
intersectM1M2
.set1 [100 x 1] logical
.set2 [150 x 1] logical
Thank you all,
Chris

1 Comment

Why are you looking for a logical result and what does it mean? For example, does intersectM1M2.set1 give the binary mask of model1.set1 values that reside in the intersection or is it the binary map of model2.set? Naturally, they could be different.

Answers (2)

No, there is no smarter way than looping, assuming smarter means faster. Even structfun, when applicable, is slower than a loop.
model1=struct('set1',randi(100,100,1),'set2',randi(100,150,1));
model2=struct('set1',randi(100,100,1),'set2',randi(100,150,1));
model3=struct('set1',intersect(model1.set1,model2.set1),'set2',intersect(model1.set2,model2.set2))

2 Comments

This makes sense, but does not give a logical mask output as requested by the OP.
For logical result
model1=struct('set1',randi(100,100,1),'set2',randi(100,150,1));
model2=struct('set1',randi(100,100,1),'set2',randi(100,150,1));
[~,idx1]=intersect(model1.set1,model2.set1)
[~,idx2]=intersect(model1.set2,model2.set2)
A1=false(100,1)
A1(idx1)=true;
A2=false(150,1);
A2(idx2)=true;
model3=struct('set1',A1,'set2',A2)

This question is closed.

Products

Tags

Asked:

on 18 Nov 2012

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!