cell comparison with other cells in different rows
이전 댓글 표시
I have a 6x2 cell array. Each cell is a 1x2 cell that contains an x and y co-ordinate. I want to be able to compare the contents of each cell of every row with the contents of each cell of the other rows. Each row of the cell array stands for a line. Essentially what I need to do is to determine which rows are connected together by finding the common endpoint.
I saw something on here that I think I can utilize. It's called nchoosek. This function basically finds all possible rows combination's for a give size. The following code uses this function. The problem that I am having is that I get all zero's for the keys.
endpoints = {{-24.7697910000000,-15.8191235000000},{-20.6771670000000,-3.54125200000000};{-12.6771670000000,20.4587480000000},{-20.6771670000000,-3.54125200000000};{-11.9803417500000,-14.5401785500000},{13.0196582500000,-12.0401785500000};{-11.9803417500000,-14.5401785500000},{-24.7697910000000,-15.8191235000000};{4.32283300000000,-1.04125200000000},{-12.6771670000000,20.4587480000000};{4.32283300000000,-1.04125200000000},{13.0196582500000,-12.0401785500000}};
comparisons = nchoosek(1:size(endpoints,1),2);
N = size(comparisons,1);
keys = cell(N,1);
for j = 1:N
keys{j}=isequal(endpoints{comparisons(j,1),:},endpoints{comparisons(j,2),:});
end
채택된 답변
추가 답변 (1개)
Andrei Bobrov
2012년 4월 11일
[EDIT] :)
ep = {{-24.7697910000000,-15.8191235000000},{-20.6771670000000,-3.54125200000000};
{-12.6771670000000,20.4587480000000},{-20.6771670000000,-3.54125200000000};
{-11.9803417500000,-14.5401785500000},{13.0196582500000,-12.0401785500000};
{-11.9803417500000,-14.5401785500000},{-24.7697910000000,-15.8191235000000};
{4.32283300000000,-1.04125200000000},{-12.6771670000000,20.4587480000000};
{4.32283300000000,-1.04125200000000},{13.0196582500000,-12.0401785500000}};
M = cell2mat(cellfun(@(x)cell2mat(x),ep,'un',0));
cmb = nchoosek(1:size(M,1),2);
ic = arrayfun(@(i1)any(ismember(M(cmb(i1,1),:),M(cmb(i1,2),:))),(1:size(cmb,1))');
pout = cmb(ic,:);
댓글 수: 4
Harold
2012년 4월 11일
Harold
2012년 4월 11일
Richard Brown
2012년 4월 11일
If you're only calling nchoosek with k of 2, you can go much higher than 15. The number of possible combinations is just n*(n-1)/2. And if nchoosek is too slow (once you get into the thousands), then you can easily do it with two nested loops.
Harold
2012년 4월 12일
카테고리
도움말 센터 및 File Exchange에서 Mathematics에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!