필터 지우기
필터 지우기

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

조회 수: 1 (최근 30일)
elisa ewin
elisa ewin 2017년 6월 7일
답변: Guillaume 2017년 6월 7일
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

채택된 답변

Guillaume
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개)

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by