-1

I have a set of Vectors Ai such that i = 1...N; where N can be really large andd vectors contains integers except 0. All vectors are in same length so that's good. I need a function of which the output is a cell array C (the data class is not necessarily cell btw) such that C indices are actually the vector elements and the cell contents are the i indices for A vectors that shares the content.

F : Ai --> C

Example:

A1 = [1 2 4], 
A2 = [3 4 5], 
A3 = [4 1 2]

the resulting C should be

C{1} = [1 3];
C{2} = [1 3];
C{3} = [2];
C{4} = [1 2 3];
C{5} = [2];

and of course I would like to avoid elementwise for loops.

bop
  • 435
  • 1
  • 3
  • 11
  • 1
    You've described your problem quite clearly. The SO community now expects you to show your code and to ask a question. You've done neither. *I need a function* isn't a question. – High Performance Mark Oct 11 '13 at 12:23
  • The problem is I do not have any code. I just have that input array and my question is how to do that in a function. I am not expecting a prepared function that I can use. A hint even will do it for me. – bop Oct 11 '13 at 12:30
  • I think the asker is not really looking for intersections, but rather combinations. – Dennis Jaheruddin Oct 11 '13 at 12:46
  • Yeah maybe the header should be `which elements in the vector space are shared by which vectors?` or smth like that. – bop Oct 11 '13 at 12:51
  • @bop Can you explain more clearly what your resulting `C` is supposed to be? Maybe reduce the size of your example `A`s and give the complete output of `C`? – Dan Oct 11 '13 at 13:13

2 Answers2

1

I misunderstood the first time, I believe this is correct now:

A = [1 2 4
     3 4 5
     4 1 2];


Av = num2cell(unique(A(:))', 1);
C = cellfun(@(x)(find(any((A == x)'))), Av, 'UniformOutput', false)

Results in

C{1} = [1 3];
C{2} = [1 3];
C{3} = [2];
C{4} = [1 2 3];
C{5} = [2];
Dan
  • 45,079
  • 17
  • 88
  • 157
0

Not really sure whether I understand your goal correctly, but I think this will get you there most of the way:

Create all combinations with 1 element from the first vector, 1 from the second, and 1 from the third vector.

[X,Y,Z] = ndgrid([1 2 3 4 5], [3 4 5 6 7], [4 8 9 11 2])
[X(:) Y(:) Z(:)]

Knowing this it should not be too hard to figure out how to get all combinations between 2 vectors. and then adding all single elements should be trivial.

You may want to sort this to the second dimension and take all unique values.

Dennis Jaheruddin
  • 21,208
  • 8
  • 66
  • 122
  • This will be very memory intensive right? I have 100 - 1000 1 x 8 vectors with a vector space of 200 - 10000 elements. – bop Oct 11 '13 at 12:55
  • @bop Vectorized solutions typically consume more memory to gain speed. However, regardless of the approach, I would think your output size is going to be huge so you may be running in to problems either way if you want to get all combinations at once. If you don't need them all at once, probably a loop is going to be the least memory intensive. – Dennis Jaheruddin Oct 11 '13 at 13:02