问题
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.
回答1:
alpha
and beta
gets T
and T2
respectively. Then you are performing columnwise subtraction between T
and T2
and not a subtraction for each element of a column in T
against all elements of the same column number in T2
. If you would like to perform the latter, you are most probably needed to change the codes from ground up and getting rid of concatenations and extending into multi-dimensional arrays.
Continuing with what you have in your codes, you can at most get joint alpha-beta
indices and not separate indices for alpha
and beta
.
Thus, you can have something like -
[minindex_alongL2, minindex_alongL1, minindex_alongalphabeta] = ind2sub([size(L2,1) size(L1,1) numel(alpha)],p)
Edit 1: Assuming you are inputting values for L1
, L2
, value1
, value2
, value3
and row vectors alpha
and beta
, see if this code works for you -
lib1 = bsxfun(@minus, L1(:,1)',L2(:,1)); %%//'
lib2 = bsxfun(@minus, L1(:,2)',L2(:,2)); %%//'
lib3 = bsxfun(@minus, L1(:,3)',L2(:,3)); %%//'
t1 = cat(3,lib1,lib2,lib3);
t2 = permute(alpha,[4 3 1 2]);
T = bsxfun(@times,t1,t2);
t22 = permute(beta,[4 3 1 2]);
T2 = bsxfun(@times,t1,t22);
gama1 = value1;
gama2 = value2;
gama3 = value3;
mat1 = bsxfun(@minus,T,permute(T2,[1 2 3 5 4]));
mat2 = bsxfun(@minus,mat1,permute([gama1;gama2;gama3],[3 2 1]));
mat2 = squeeze(sum(mat2,3));
[d,p] = min(mat2(:));
d = sqrt(d);
[minindex_alongL2, minindex_alongL1, minindex_alongalpha, minindex_alongbeta] = ind2sub([size(L2,1) size(L1,1) numel(alpha) numel(beta)],p)
来源:https://stackoverflow.com/questions/24256978/finding-index-positions-after-spatial-matrix-multiplication-bsxfun-implemente