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!