I am trying to take a matrix like
A=[1 2 3 4; 5 6 7 8; 9 10 11 12]
and find a new matrix B such that
B = [A(:,1)-A(:,2), A(:,1)-A(:,3), A(:,1)-A(:,4), A(:,2)-A(:,3), A(:,2)-A(:,4), A(:,3)-A(:,4)]
(note that I'm ignoring the symmetric terms like A(:,2)-A(:,1) since that is just -(A(:,1)-A(:,2)) )
Is there any way to efficiently vectorize this process? My current method of doing a loop as below is slow
M=length(A(:,1))N=length(A(1,:))cnt=1;for i=1:N-1 for j=i+1:N B(:,cnt) = A(:,i)-A(:,j); cnt=cnt+1; endend
Best Answer