Intersection of several sets of results corresponding to different events in matlab

I have a matrix (5x10000) with the fifth line contains values between 1 and 50 corresponding to different events of an experiment. My goal is to find the columns of the matrix which are the same for different events. In other words, I want the columns results for all possible combinations of different events (subsets of {1,2, .., 50}) (For example: {1,3,7} and {7,1,3} are of course the same combination). It sounds like a problem of intersection of sets that each contains all possible outcomes for a given event. I hope also that the computation time is reasonable.
example with a matrix (5x20):
A =
20 4 4 74 20 20 3 1 1 4 3 3 3 7 4 1 20 3 3 74
36 1 1 11 36 36 3 3 3 1 3 3 3 9 4 3 36 4 3 11
77 1 1 15 77 77 1 3 3 1 1 1 1 10 3 2 77 4 1 15
9 4 4 40 9 9 2 4 4 4 2 2 2 40 1 4 9 3 2 40
3 4 2 6 7 3 4 5 2 7 4 2 7 6 7 2 5 5 1 3
in this case we have seven different events from 1 to 7: line 5
for example:
the intersection of the results of events 3, 5 and 7 is the vector: [20 36 77 9]'
the intersection of the results of events 1, 2, 4 and 7 is the vector: [3 3 1 2]'
the intersection of the results of events 3 and 6 are the vectors: [20 36 77 9]' and [74 11 15 40]'
So what I want is the common columns for a specified number of different events between 1 and 50. For example, how to get the columns common to 20 different events? The problem becomes more complicated for me when I think to find this result for all possible combinations of 20 events in the set {1,2,..., 50}.
I hope to have the common columns for all possible combination for a given number of different events, but I gave the number 20 just as an example on which to base one solution.
I'll rephrase my question that to make it clearer:
the following matrices are sub-matrix of A, each corresponding to a given event:
A1= [3;3;1;2;1]
A1 corresponds to results of the event 1
A2= [4 1 3 1;1 3 3 3;1 3 1 2;4 4 2 4;2 2 2 2]
A2 corresponds to results of the event 2
A3= [20 20 74;36 36 11;77 77 15;9 9 40;3 3 3]
A3 corresponds to results of the event 3
A4= [4 3 3;1 3 3;1 1 1;4 2 2;4 4 4]
A4 corresponds to results of the event 4
A5= [1 20 3;3 36 4;3 77 4;4 9 3;5 5 5]
A5 corresponds to results of the event 5
A6= [74 7;11 9;15 10;40 40;6 ]6
A6 corresponds to results of the event 6
A7= [20 4 3 4 7;36 1 3 4;77 1 1 3;9 4 2 1;7 7 7 7]
A7 corresponds to results of the event
my goal is to find intersection along the columns of the matrix Ai (1:4,:) i = 1,2, ... 7
in other words:
intersection(Ai,Aj)(1:4,:) for i and j different
intersection(Ai,Aj,Ak)(1:4,:) for i,j and k different
intersection(Ai,Aj,Ak,Al)(1:4,:) for i,j,k and l different
intersection(Ai,Aj,Ak,Al,Am)(1:4,:) for i,j,k,l and m different
intersection(Ai,Aj,Ak,Al,Am,An)(1:4,:) for i,j,k,l,m and n different
intersection(Ai,Aj,Ak,Al,Am,An,Ao)(1:4,:) for i,j,k,l,m,n and o different
intersection(Ai,Aj,Ak,Al,Am,An,Ao,Ap)(1:4,:) for i,j,k,l,m,n,o and p different
when I say "intersection (Ai, Aj)(1:4,:) for i and j different," I want the columns common to the matrix Ai(1:4,:) and Aj(1:4,:)
the result for each intersection can be many column vectors, not necessarily one, depending on the columns of the matrix A.
I hope that each result contains the vector column of the matrix Ai (1:4,:) followed by the corresponding values of events, such as: if [3 3 1 2]' is the intersection of A1, A2, A4 and A7, I want to get as a result the vector [3 3 1 2 1 2 4 7]'
for example: intersection(A1,A2,A3,A4)(1:4,:): my goal is to avoid the following loop:
[n1 m1] = size(A1);
[n2 m2] = size(A2);
[n3 m3] = size(A3);
[n4 m4] = size(A4);
k=1;
for i1=1:m1
for i2=1:m2
for i3=1:m3
for i4=1:m4
if A1(1:4,i1)==A2(1:4,i2) && A2(1:4,i2)==A3(1:4,i3) && A3(1:4,i3)==A4(1:4,i4)
intersection1234(:,k) = [A1(1:4,i1);A1(5,i1);A2(5,i2);A3(5,i3);A4(5,i4)];
k=k+1;
end
end
end
end
end

Réponses (1)

It's hard to understand what you want. I'll take a shot.
A=[20 4 4 74 20 20 3 1 1 4 3 3 3 7 4 1 20 3 3 74
36 1 1 11 36 36 3 3 3 1 3 3 3 9 4 3 36 4 3 11
77 1 1 15 77 77 1 3 3 1 1 1 1 10 3 2 77 4 1 15
9 4 4 40 9 9 2 4 4 4 2 2 2 40 1 4 9 3 2 40
3 4 2 6 7 3 4 5 2 7 4 2 7 6 7 2 5 5 1 3];
A4=A(1:4,:).';
[UniqueA4,IndI,IndJ]=unique(A4,'rows');
N=size(UniqueA4,1);
EventCollection=cell(N,1);
for k=1:N
EventCollection{k}=A(:,IndJ==IndJ(IndI(k)));
end
celldisp(EventCollection);
EventCollection{1} =
1
3
2
4
2
EventCollection{2} =
1 1
3 3
3 3
4 4
5 2
EventCollection{3} =
3 3 3 3 3
3 3 3 3 3
1 1 1 1 1
2 2 2 2 2
4 4 2 7 1
EventCollection{4} =
3
4
4
3
5
EventCollection{5} =
4 4 4
1 1 1
1 1 1
4 4 4
4 2 7
EventCollection{6} =
4
4
3
1
7
EventCollection{7} =
7
9
10
40
6
EventCollection{8} =
20 20 20 20
36 36 36 36
77 77 77 77
9 9 9 9
3 7 3 5
EventCollection{9} =
74 74
11 11
15 15
40 40
6 3

15 commentaires

Results should be column vectors of the initial matrix.
Is it your desired output? See updated answer.
Thank you very much for your reply, I added some detail to my question to make it clearer. Thank you again.
I am lost. Let's start from the simple case.
"intersection(Ai,Aj)(1:4,:) for i and j different"
You want the 1 to 4 row of data to be common between two different event, i and j, right? So that will be EventCollection{2} and EventCollection{9} in the answer, right? Don't worry about the format. Just confirm whether or not that is the expected result.
in the matrix A, there are no event 9. and in the question, I seek the intersection between the results of events and not between the sets of events that give one result. There is no intersection between EventCollection{i} and EventCollection{j} for i and j different.
when I say "intersection (Ai, Aj)(1:4,:) for i and j different," I want the columns common to the matrix Ai(1:4,:) and Aj(1:4,:)
Please, look the part added at the end of my question. Thank you.
I don't get it. You start with 5x20 matrix, but now you are talking about the A1 to A7 matrix. Can you stick with one format. Regarding "intersection(Ai,Aj)(1:4,:) for i and j different", is the result in EventCollection{2} what you are looking for? It is the common data [1;3;3;4] which is common between event 2 and event 5, right? Samething for EventCollection{9}. The result in EventCollection{5} will be the common data for event 4, 2 and 7. I really try to understand your question.
Ai i=1,2,,..,7 are sub-matrix of the 5x20 matrix A.
Okay, let's stick with the 5x20 matrix. Now is the following result correct for "intersection of event data for two different events"?
1 1
3 3
3 3
4 4
5 2
and
74 74
11 11
15 15
40 40
6 3
If that is the case, then take a look at the 9 cells in EventCollection in the answer. Some cells have only one column, which means no other event has the same data. Some cells have two columns, which is for common between two events. Others have 3, 4, or 5 columns. I noticed that there are duplicated columns, but that can be taken care of.
If this is the correct result, then the rest is to go through a for-loop with the cells, count the number of unique columns and spit out the output format as you want. In this case, for two events, there are two intersections. For three events, there are three intersections (aftger removing the duplicated column). But for others such as 5, 6 or 7 events, there is no intersection. Is this expected?
thank you very much for the effort you take to respond to my question. I had an answer, and I hope to change it. you can see my second question here: "how I can change this multi-loop to reduce the computation time in Matlab"
That is fine. I run your code using the A matrix in this example. I got result if running for-loop i=1:20 but I could not understand the result "intersection". If I run for-loop i=1:50, I had the error "??? Index exceeds matrix dimensions."
Once you get the correct result, let me know. I can run the data against the example A matrix. I just want to understand the logic behind it.
the matrix A (5x20) was only an example to understand my question, but I use a Matrix (5x10000) for my program.
That might be the issue. You have so many nested for-loops and it won't be wise to test it on a 5x10000 matrix right away. Why don't you try it on the 5x20 matrix first and make sure your code is right.

Connectez-vous pour commenter.

Catégories

Tags

Aucun tag saisi pour le moment.

Question posée :

le 13 Déc 2011

Community Treasure Hunt

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

Start Hunting!

Translated by