find the number of times a pair of value is a struct
1 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Guillaume
2017-6-7
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.
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Structures 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!