1

I have a two cell arrays R and C (two vectors with R(n-elements), C(m-elements)) and my task is to compare each element of R with each element of R and each element of C with each element of C. Comparison is to finding intersection of two cells. In result I want to obtain two matrices. One matrix Q for R nxn, where in cell Q(i,j) is intersection of two elements R(i) and R(j) and second matrix P for C mxm, where in cell P(i,j) is intersection of two elements C(i) and C(j). Generally I can do this using two for-loops, but my data is quite big and I wonder if there is any method to speed up the computation? The first idea was to replace the cell array, where in each cell are the indexes of rows (vector R) or columns (vector C) which I want to compare (rows and columns of binary matrix BM, BM is input data) . So If R(1) = {2 3 4}, and BM is 5x5, then R(1,:)=[0 1 1 1 0]. Now having this binary matrix R I could compare each row with each row only with one loop. But then I still need to come back to number of rows eg

R(1,:) = [0 1 1 1 0];
R(2,:) = [0 1 1 0 0]; %then 
Q(1,2) = [0 1 1 0 0]; %(intersection of element R(1) and R(2)) and

C(1,:) = [1 1 0 0 0]; 
C(2,:) = [1 0 0 1 0]; %then
P(1,2) = [1 0 0 0 0]; % Now I want to obtain 
Results(i,j) = sum(BM(Q(1,2),P(1,2)))=sum(BM([2 3],[1]));

Do you have any idea how to cope with this, and compare two vectors of cell array without a two loops?

2
  • If all input data vectors have the same dimensions, you don't need cell arrays. You'd rather use matrices, which are faster and easier to manipulate. Commented Jul 8, 2013 at 9:22
  • Unfortunately the vectors don't have the same dimensions. I thought that if I replace cells {1 2 5} with binary vector {1 1 0 0 1}, (where the indexes of elements equal to 1, represent elements from cell) I will speed up the computation, becouse all calculation will be perform on matrices but the data are too big and it caused the out of memory error. Commented Jul 11, 2013 at 6:37

1 Answer 1

1

Since Q( k, l ) is a vector with numCols (5 in your example) it cannot be stored in a 2D matrix Q: Q should either be a 2D cell array, or a 3D matrix.

Using the binary matrix directly to obtain Q (row intersections):

>> Q = bsxfun( @times, permute( BM, [1 3 2] ), permute( BM, [3 1 2] ) );

Now, Q( k, l, : ) holds the intersection betwee the k-th and l-th rows of BM.
Same goes for P:

>> P = bsxfun( @times, permute( BM, [3 2 1] ), permute( BM, [2 3 1] ) );
Sign up to request clarification or add additional context in comments.

2 Comments

Of course You're right! I applied bsxfun to my dataset but it's so big that it kill matlab (I have an out of memory error). Anyway Thank you for your tip, in other case it would be very helpful.
@karolina it would help if you give as much detail as possible about your data, so that the provided answers can be more helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.