I need help finding some index-positions of a matrix and two vectors after a complicated matrix multiplication, please bear with me and read what I have first, my question comes at the end.
I have two matrices L1
and L2
:
L1 = firstMatrix;
L2 = secondMatrix;
I need to compute the difference (column-wise) of every single value from L1
with all the values of L2
, again, in column-wise form, this is done as follows:
step one
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(:);
At the end I have my new matrix LBR
:
LBR = [lib1 lib2 lib3];
Now, I have two vectors alpha
and beta
-given on a closed domain with same step-size, in this case they are the same-.
alpha = 0:0.1:2;
beta = 0:0.1:2;
I need now to compute the tensor-product, I can do it in two ways:
step two
first way:
alphat1 = kron(alpha,lib1);
alphat2 = kron(alpha,lib2);
alphat3 = kron(alpha,lib3);
T1 = [alphat1 alphat2 alphat3];
betat1 = kron(beta,lib1);
betat2 = kron(beta,lib2);
betat3 = kron(beta,lib3);
T2 = [betat1 betat2 betat3];
Or, the second way, which is by means of the bsxfun
from matlab:
val = bsxfun(@times,LBR,permute(alpha,[3 1 2]));
T = reshape(permute(val,[1 3 2]),size(val,1)*size(val,3),[]);
val2 = bsxfun(@times,LBR,permute(beta,[3 1 2]));
T2 = reshape(permute(val2,[1 3 2]),size(val2,1)*size(val2,3),[]);
My problem:
I need to find the min-distance
in the following way, first, I have of course three constants:
gama1 = value1;
gama2 = value2;
gama3 = value3;
And the min-distance
is computed as follows:
step three
[d,p] = min(((T(:,1)-T2(:,1))-gama1).^2 + ((T(:,2)-T2(:,2))-gama2).^2 +
((T(:,3)-T2(:,3))-gama3).^2);
d = sqrt(d);
I need, very much, the index positions of L1
, L2
, alpha
and beta
which are fulfilling this min-distance
problem.
I have tried the following:
step four
[minindex_alongL2, minindex_alongL1, minindex_alongalpha, minindex_alongbeta] =
ind2sub([size(L2,1) size(L1,1) numel(alpha) numel(beta)],p);
But it doesn't work. I would appreciate very much all the help you can provide me!
Thanks in advance.
bsxfun
questions! I really tried to find a solution, believe me I do, but when I am posting my questions here is because I really need help. So if you have any suggestions! The only difference this case with the case before: stackoverflow.com/questions/24181012/… is that I have two vectors which I am tensor-multiplying. – Sergio HaramOP
and it will help me to get a good answer too! So, yes! it is worthy to spend time writing good questions :) – Sergio Haram