问题
I really need help with this one.
I have to matrices L1
and L2
, both are (500x3)
of size.
First of all, I compute the difference of every element of each column of L1
from L2
as follows:
lib1 = bsxfun(@minus, L1(:,1)',L2(:,1));
lib1=lib1(:);
lib2 = bsxfun(@minus, L1(:,2)',L2(:,2));
lib2=lib2(:);
lib3 = bsxfun(@minus, L1(:,3)',L2(:,3));
lib3=lib3(:);
LBR = [lib1 lib2 lib3];
The result is this matrix LBR
. Then I have a min
-problem to solve:
[d,p] = min((LBR(:,1) - var1).^2 + (LBR(:,2) - var2).^2 + (LBR(:,3) - var3).^2);
Which returns the point p
where this min
-problem is fulfied. Finally I can go back to my matrices L1
and L2
to find the index-positions of the values which satisfy this min
-problem. I done this as follows:
[minindex_alongL2, minindex_alongL1] = ind2sub(size(L1),p);
This is OK. But what I need now is:
I have to multiply , take the tensor-product
, also called Kronecker product
of a vector called alpha
to LBR
, alpha
is given as follows:
alpha = 0:0.1:2;
And, this Kronecker product
I have computed as follows:
val = bsxfun(@times,LBR,permute(alpha,[3 1 2]));
LBR = reshape(permute(val,[1 3 2]),size(val,1)*size(val,3),[]);
what I need now is: I need to solve the same min
problem:
[d,p] = min((LBR(:,1) - var1).^2 + (LBR(:,2) - var2).^2 + (LBR(:,3) - var3).^2);
but, this time, in addition of finding the index-positions and values from L1
and L2
which satisfies this min
-problem, I need to find the index position of the single value from the alpha
vector which has been multiplied and which fulfills the min
-problem. I don't have idea how can I do this so any help will be very appreciated!
Thanks in advance!
Ps: I can post the L1
and L2
matrices if needed.
回答1:
I believe you need this correction in your code -
[minindex_alongL2, minindex_alongL1] = ind2sub([size(L2,1) size(L1,1)],p)
For the solution, you need to add the size of p
into the index finding in the last step as the vector whose min
is calculated has the "added influence" of alpha
-
[minindex_alongL2, minindex_alongL1,minindex_alongalpha] = ind2sub([size(L2,1) size(L1,1) numel(alpha)],p)
minindex_alongalpha
might be of your interest.
来源:https://stackoverflow.com/questions/24181012/bsxfun-implementation-in-solving-a-min-optimization-task