2
votes

I have a sort of strange question, but here goes. I have a matrix containing (T11, T12, T21, and T22)...

| T11 T12 |
| T21 T22 |

I have a function that will return a scalar value upon comparison of two values in the matrix. Lets call it f(x,y). And I want to run this function for each two values so I do...

f(T11,T12), f(T11,T21), f(T11,T22),
f(T12,T21), f(T12,T22),
f(T21,T22)

I store these results in a vector, now containing 6 elements

V1 = [f(T11,T12), f(T11,T21), f(T11,T22), f(T12,T21), f(T12,T22), f(T21,T22)]

What I want to do now, is find out which f(x,y) corresponds to which two x,y values. So the vector V1 will contain values, for example:

V1 = [12 14 54 23 86 3]

So you can see that the first index (i=1) of V1 has a value of 15, and corresponds to f(T11,T12), and that the third index (i=3) of V1 = 54, and corresponds to f(T11,T22).

If anything is unclear please let me know. To refresh, I want to be able to determine the original values input into f(x,y) for each value in V1. I have tried looking for patterns and so far have been unable to come up with anything, I forgot a bunch of math I used to know...I'm thinking there is a relationship between the indicies of V1 and the indicies compared in the matrix. Ideas please! (p.s. I also tried including the indicies along with the values in the function return, but its really messy and I would rather do it a fancy mathematical way.

3

3 Answers

0
votes

Ah here we go. I have at last come to a method of solving this tricky question!! So you want to find the original values from the matrix given an index in V1 = index. What I did is get the size of the matrix (m,n) create a row=1, col=1, and then

for i = 1 to (m*n) 
    if index - (m*n) plus i is <= 0, then col=index
    stop
else
    index = index - mn + i
row = row + 1

Now you can flatten the matrix to a 1 by m*n and the two values you want are row and row+col

0
votes

I suggest creating two indexing matrices with meshgrid:

N = numel(T);
[ind1,ind2]=meshgrid(1:N);

>>ind1 =

   1   2   3   4
   1   2   3   4
   1   2   3   4
   1   2   3   4

>>ind2 =

   1   1   1   1
   2   2   2   2
   3   3   3   3
   4   4   4   4

and then use the strict upper or lower triangular part of these to linear index into T:

selection = logical(tril(ones(N),-1)
ind1 = ind1(selection);
ind2 = ind2(selection);

>>ind1' = 
   [1 1 1 2 2 3]
>>ind2' = 
   [2 3 4 3 4 4]

So now you can simply do:

V1 = arrayfun(@f,T(ind1),T(ind2));

and now ind1 and ind2 contain the row and column index into T of the values in V1.

I suggest executing these lines of code step by step and inspecting intermediary variables to see what everything does.

0
votes

The order in which you are taking elements out of the matrix (suppose it is named A, and has dimensions of m rows and n columns) for use in your function f(x,y), is the same as if you were looking at the vector (let us assume m = 2, A(1,:) is the first row of the matrix A)

w = [ A(1,:) A(2,:)]

and taking all of its combinations (assuming none of the entries in matrix A are the same) by taking the leftmost element of the vector and comparing it with the all of the elements to its right (in order from left to right), and so forth until you had generated all combinations of size 2 from the vector w. I the matrix A was defined as followed

A = [ 1 2; 3 4]

The generated combinations of size 2 of the entries of A would be (note that the combinations are represented as the rows of the following matrix B)

B = [1 2; 1 3; 1 4; 2 3; 2 4; 3 4]

We know that there are m*n entries in matrix A (in this case 4). In the vector V1 from your question we know that the first (m*n - 1) entries all have the first element of the vector w as the first input into f(x,y), and the next (m*n - 2) entries have the second element of the vector w as the first input into f(x,y) and so on. The following code should give you your answer if index is equal to the index in V1

p = 1
for i = (m*n - 1):-1:1
  for j = 1:i
    index = index - 1
    if (index == 0)
      disp('The following number is the position of x in vector w')
      disp(p)
      pw = p
      disp('The following number is the position of y in vector w')
      disp(p + j)
      qw = p + j
  p = p + 1

disp('The row of input x of f(x,y) in A is the following:')
disp(idivide(int32(pw - 1),n))
disp('The column of input x of f(x,y) in A is the following:')
disp(pw - (idivide(int32(pw - 1),n)*n))
disp('The row of input y of f(x,y) in A is the following:')
disp(idivide(int32(qw - 1),n))
disp('The column of input y of f(x,y) in A is the following:')
disp(qw - (idivide(int32(qw - 1),n)*n))