Clear Filters
Clear Filters

find the number of times a pair of value is a struct

1 view (last 30 days)
Hi, I have a struct of data Clusters3 with dimension [22x1]. I'm interested to find if the pair of value in o (attached)[6 2] are presents in the first two column of Clusters3.users. I want as output a matrix A [22 6] in which is represented for every element of Cluster3 if the six pairs of value in present or no in Clusters3.users
Example
A(1,1) hold the number of times the pair in o(1) is present in Clusters3(1).users(:,1:2);
A(1,2) hold the number of times the pair in o(2) is present in Clusters3(1).users(:,1:2);
A(1,3) hold the number of times the pair in o(3) is present in Clusters3(1).users(:,1:2);
etc
I have tried
for i=1:size(o,1)
c(i)=find (Clusters3(1).users(:,1:2)==o(i))
end
but it gives errors.
I have also try
structfun(@find, Clusters3.users, o(1))
but it doesn't run
Can you help me? thanks

Accepted Answer

Guillaume
Guillaume on 7 Jun 2017
One of many ways to do this:
A = zeros(size(Clusters3, 1), size(o, 1));
for crow = 1:numel(Clusters3)
for orow = 1:size(o, 1)
A(crow, orow) = sum(ismember(Clusters3(crow).users(:, [1, 2]), o(orow, :), 'rows'));
end
end
You may want to use ismembertol with an appropriate tolerance instead of ismember since you're comparing floating point values.
Your example data is not very useful since only cluster3(1) has any match.

More Answers (0)

Categories

Find more on Structures in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!