Question

I have about 1000 columns of data. I want to create another array comprising the differences between all possible combinations of the columns for all rows of data. I can go about it using afor loop, but is there an easier way in MATLAB to do this?

Data is in the following format

Date Col1 Col2 ..... Col1000
.     .   .              . 
.     .   .              .

I need the final array to be in the following format

Date Col1-Col2 Col1-Col3 .... Col1-Col1000 Col2-Col3 Col2-Col4 ..... Col2-Col1000

and continuing to Col999-Col1000 for a total of nchoosek(1000,2)+1 columns of data plus the dates.

Was it helpful?

Solution

bsxfun can virtually replicate in more than one dimension, allowing you to avoid a loop (sorry naysayers and bitter down-voters, it's true):

P = 50; N = 1000; A = rand(P,N);

% bsxfun for singleton expansion: 50x1000x1 @minus 50x1x1000 => 50x1000x1000
B = bsxfun(@minus,A,permute(A,[1 3 2]));
B = reshape(-B,size(A,1),[]);

That gives a P-by-N*N matrix which provides all the combinations. To get the thinned out (i.e. with sum(1:N-1) columns) matrix:

m = logical(tril(ones(N),-1)); % lower triangular logical not including diagonal
B = B(:,m(:));

Finally, concatenate the dates to get what you need:

C = [dates B];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top