2

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.

Divakar
  • 218,885
  • 19
  • 262
  • 358
Sergio Haram
  • 437
  • 4
  • 17
  • Hi @Divakar, I am doing what you asked me for :D giving you more `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: http://stackoverflow.com/questions/24181012/bsxfun-implementation-in-solving-a-min-optimization-task is that I have two vectors which I am tensor-multiplying. – Sergio Haram Jun 17 '14 at 06:27
  • 1
    +1 For the well formulated question. Understandability is well above average. – NoDataDumpNoContribution Jun 17 '14 at 08:48
  • 1
    Thank you very much @Trilarion :) I know that a good question is very appreciated at the `OP` and it will help me to get a good answer too! So, yes! it is worthy to spend time writing good questions :) – Sergio Haram Jun 17 '14 at 09:05

1 Answers1

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)
Divakar
  • 218,885
  • 19
  • 262
  • 358
  • Hi @Divakar I don't want to loose practice so here I am, coming with a question to you, again :) In the part of your code: `mat1 = bsxfun(@minus,T,permute(T2,[1 2 3 4]));` I am having problems running a _little_ big `L1, L2 alpha and beta` vectors. I get from `matlab`: `Error using bsxfun. Out of memory. Type HELP MEMORY for your options. Error in (line 97) mat1 = bsxfun(@minus,T,permute(T2,[1 2 3 5 4]));` Any idea how could I fix this issue? In advance thanks. – Sergio Haram Jun 23 '14 at 08:41
  • `L1 and L2` are `(1000x3)` each and `alpha beta` are `(1x50)` each. May be there is a way to transpose `alpha and beta`, will this help? – Sergio Haram Jun 23 '14 at 08:42
  • @SergioHaram `bsxfun` is known to be memory hungry and you are running into the same. Not sure if there's a way around it when sticking to `bsxfun`. – Divakar Jun 23 '14 at 08:44
  • I see, well, I will try to reduce the size of the inputs parameters and see how it goes. :) – Sergio Haram Jun 23 '14 at 08:46
  • I believe I may be force to see another options here, since this `kron` product I am computing: `L1` and `alpha` for example, give just a vector of dim `50 000 x 3` which is not so big... :( May be I can split the vectors in, let's say three? may be this will just complicate things even more... – Sergio Haram Jun 23 '14 at 08:51
  • @SergioHaram Exactly, splitting could be done, but then too many complications there with splitting + rejoining. How about `kron`? Is it working with such large datasets? – Divakar Jun 23 '14 at 09:55
  • my friend I have reformulated the problem... I have a new question which, I hope, shows the min distance problem in a more general way so it can be "attacked" from different angles. The question is here: http://stackoverflow.com/questions/24363552/iteration-of-matrix-vector-multiplication-which-stores-specific-index-positions PS `kron` works fine – Sergio Haram Jun 23 '14 at 10:22
  • Hi!, I am bothering you again. I just have a little question. In the step three of my question you can see that I am squaring every element in the sum, is this done in your answer-code? because I cannot find it, may be it should be `mat2 = squeeze(sum(mat2,3).^2)`; ? or Am I wrong? thanks! :) PS. the reason I have noticed this is because I am getting d which is the distance, with imaginary numbers(!) – Sergio Haram Jun 26 '14 at 08:22
  • @SergioHaram Hey again!:) Well it must be `mat2 = squeeze(sum(mat2.^2,3));`. Sorry about the bug there :) I promise, I would clean up the code soon! – Divakar Jun 26 '14 at 08:52