필터 지우기
필터 지우기

Building a connectivity matrix from one-to-many columns

조회 수: 6 (최근 30일)
Neil
Neil 2012년 2월 1일
Hi All.
For a simpler example of what I am doing - I have two columns of data. They are both numbers, both 'n' in size. Say column A and column B.
Each element in column A, maps to the corresponding location in column B. The elements are not unique - one number will occur many times in column A, for instance, each time mapping to a unique value in column B (there are no repetitions).
For a given element in A, I want to find all of the corresponding mapping points in B.
I then want to build a matrix, where rows and columns correspond to IDs from A. The (i,j) position will then be equal to 1, if the i,j'th elements in A have a common mapping point in B.
For a small example,
A = [1 1 2 2 2 3 3] B = [a b a c d b d]
Then, '1' has a map in common (1 goes to a and b, and so on) with '2' - 'a' and '3' - 'b'. and '2' has a common point with '3' - 'd'. Then, the matrix would be:
(1,2) = 1, (2,3)=1, (1,3)=1. (it will be symmetric, so the order (1,2) or (2,1) doesn't matter).
I can do this with lots of loops, but if someone has a better way I would be thankful.
Neil.
  댓글 수: 3
Walter Roberson
Walter Roberson 2012년 2월 2일
1,3 = 1 because 1 maps to b (position 2) and 3 maps to b (position 6)
Neil
Neil 2012년 2월 6일
As Walter says.
I thought I had an answer with a plethora of loops, but it seems I keep making mistakes. Hopefully I can still find an answer!
I think whether or not B contains letters or number isn't important - it is a one to many relationship from A to B that I want to express as a matrix. I could have numbers instead of letters in B, but all it all those values are simply labels for a set.
The numbers in A are the important ones - these are the values I will use as indices for my matrix. Therefore, the matrix is square with dimensions equal to the number of unique entries in A.

댓글을 달려면 로그인하십시오.

답변 (1개)

Sean de Wolski
Sean de Wolski 2012년 2월 6일
So something like:
a = pi; %numbers;
b = 2.1;
c = 1.2;
d = 42;
A = [1 1 2 2 2 3 3]'; %column vectors
B = [a b a c d b d]';
Aacc = accumarray(A,B,[],@(x){x}); %build cell array of parts
[r c] = find(triu(true(numel(Aacc)),1)); %non diagonal coords
connmat = diag(cellfun('prodofsize',Aacc)); %make diagonal
for ii = 1:numel(r);
n = sum(ismember(Aacc{r(ii)},Aacc{c(ii)})); %number intersecting
connmat(r(ii),c(ii)) = n; %save
connmat(c(ii),r(ii)) = n;
end
?

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by