matlab - Match rows of 2 matrices for a minimum total sum of row-wise differences -
i have 2 matrices equal number of rows , columns. a(4x3) , b(4x3).
i match rows of rows of b when subtracting minimum sum. example of how inside of , b like:
a= -0.3612 -0.1911 -15.3818 0.0376 0.0206 0.3498 0.0418 0.0229 0.3887 0.0188 0.0103 0.1749
i'm doing this:
for j=1:size(b,1) [vv, inds]=min(sum(abs(bsxfun(@minus,a,b(j,:))),2)); minjj(j,:)=vv; indsmatch(j,:)=inds; mm=min( minjj); [a,ps] = removerows(a,'ind',inds); [b,ps] = removerows(b,'ind',j); end
i error:
improper assignment rectangular empty matrix.
what i'm trying indsmatch
. want 'order of rows indexes' of matrix paired rows of b (based on min difference). indexes of b kept 1 2 3 4.
here's vectorized approach. tries permutations of rows of b
, , produces optimal row-permuted b
in matrix bresult
.
[m, n] = size(a); ind = perms(1:m); % // permutations of row indices bb = permute(reshape(b(ind.',:),m,[],n),[1 3 2]); %'// row-permuted b matrices c = sum(sum(abs(bsxfun(@minus, a, bb)),2),1); % // compute sum each [minsum, imin] = min(c); % // minimize bresult = b(ind(imin,:),:); % // output result
Comments
Post a Comment