bsxfun实现中的最小化解决方案.优化任务 [英] bsxfun implementation in solving a min. optimization task
问题描述
我真的需要这个帮助.
I really need help with this one.
我必须使用矩阵L1
和L2
,它们的大小均为(500x3)
.
I have to matrices L1
and L2
, both are (500x3)
of size.
首先,我计算L1
的每个列中每个元素与L2
的差值,如下所示:
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];
结果是此矩阵LBR
.然后我有一个min
问题要解决:
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);
哪个返回点p
满足该min
问题的位置.最后,我可以回到矩阵L1
和L2
来找到满足该min
问题的值的索引位置.我这样做如下:
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:
我必须相乘,取tensor-product
,也称为alpha
到LBR
的向量的Kronecker product
,alpha
给出如下:
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;
而且,我计算出的Kronecker product
如下:
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),[]);
我现在需要的是:我需要解决相同的min
问题:
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);
但是,这次,除了从满足此min
问题的L1
和L2
查找索引位置和值之外,我还需要找到单个索引的索引位置alpha
向量中的"strong"值,该值已经相乘并且满足min
问题.我不知道如何执行此操作,因此我们将不胜感激!
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!
提前谢谢!
Ps:如果需要,我可以发布L1
和L2
矩阵.
Ps: I can post the L1
and L2
matrices if needed.
推荐答案
我相信您需要在代码中进行此更正-
I believe you need this correction in your code -
[minindex_alongL2, minindex_alongL1] = ind2sub([size(L2,1) size(L1,1)],p)
对于该解决方案,您需要在最后一步中将p
的大小添加到索引查找中,因为计算出min
的向量具有alpha
-
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
可能与您有关.
这篇关于bsxfun实现中的最小化解决方案.优化任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!