I have a matrix y
and a vector x
, I need to find all the possible vectors resulted from the mapping of each value in x
into each vector in y
.
That is difficult to be understood; let's explain is with an example:
Here is an example, I have the vector x = [0.7 0.7i; 0.7-0.7i];
The matrix y = [1 0; 2 0; 1 2];
the resulted matrix is supposed to be like this one Z = [0.7 0.7i 0; 0.7-0.7i 0; 0 0.7 0.7i; 0 0.7-0.7i; 0.7 0.7i 0.7 0.7i; 0.7 0.7i 0.7-0.7i; 0.7 - 0.7i 0.7-0.7i ; 0.7 - 0.7i 0.7 0.7i];
. That is equivalent into Z = [x_1 0; x_2 0; 0 x_1; 0 x_2; x_1 x_1; x_1 x_2; x_2 x_2; x_2 x_1];
. That means it map each value in x
into the row of Z
according to the index value in y
.
Here is my try code: clear all; clc;
y = [];
G = 2;
v = 1 : G;
for i = 1: G
x=nchoosek(v,i);
m = zeros(size(x,1),G-i);
y =[y ; x m]; % creat the matrix y
end
x = [0.7 0.7i; 0.7-0.7i];
Z = []; s = zeros(G,1);
for k=1:size(x,1)
for i=1:size(y,1)
n=y(i,:);
n=n(n ~= 0);
s(n)=x(k);
Z=[Z s];
s = zeros(G,1);
end
end
The problem in my code that matrix Z
show the inverse, it means it takes the input x_1
from x
and then map it into all possible values in y
. For example the matrix Z
starts with [x_1 0; 0 x_1; x_1 x_1 ….]
, however that should be the inverse, which means takes each values in x
and map it as shown in the above example [x_1 0; x_2 0; x_3 0 …..]
. The second issue, when y
contains more than non-zeros values, my code cannot get all possible vectors, it can only get [x_1 x_1; x_2 x_2];
but I cannot map the other possibilities which are [x_1 x_2; x_2 x_1]
and so on.
How can I solve that issue?
UPDATE
Here is the updated question with clear description. I have the vector x
and matrix y
, I need to fill the matrix z
following the index taken from each row in matrix y
. For example, if the first row in matrix y
is [1 0]
or [0 1]
; then I will take all possible values from x and put it in z
following the number taken from the row in y
which is 1 in this case. Then, the same case for row 2 in matrix y
which is [2 0]
or [0 2]
; it means that second column in z will be filled with all possible values in x.
Then, the two columns in z
can be filled which is equivalent into the case [1 2]
in y
, so it will take the first value from x
and fill it with all other possible values from x
, and so on. The rows in z
should not be repeated.
The matrix Z
is exactly as shown with below answer of AboAmmar below, but using the loop if with longer vector x
and bigger matrix y
will be little bit complicated.
CodePudding user response:
As you describe it, there are 4 distinct cases for each row of y
and the corresponding output:
[0 1]
or[1 0]
=> [x 0][0 2]
or[2 0]
=> [0 x][1 2]
=> [x1 x1; x1 x2; x2 x2; x2 x1][2 1]
=> [x1 x1; x2 x1; x2 x2; x1 x2]
These don't seem to follow any obvious rule. So, the easiest (but not smartest) solution is to use if-else
and select the suitable case from the above. We don't have all the information about the possible indices, or if rows like [1 1]
and [2 2]
might happen, so the following solution is by no means exhaustive; surprising errors might happen if other inputs are fed into y
matrix.
y = [];
G = 2;
v = 1 : G;
for i = 1: G
x = nchoosek(v,i);
m = zeros(size(x,1),G-i);
y = [y ; x m]; % creat the matrix y
end
Z = [];
x = [0.7 0.7i; 0.7-0.7i]
for i = 1:size(y,1)
r = y(i,:);
if ismember(r, [1 0; 0 1], 'rows')
Z(end 1:end 2,:) = [x [0; 0]];
elseif ismember(r, [2 0; 0 2], 'rows')
Z(end 1:end 2,:) = [[0; 0] x];
elseif ismember(r, [1 2], 'rows')
Z(end 1:end 4,:) = [x(1) x(1); x(1) x(2); x(2) x(2); x(2) x(1)];
elseif ismember(r, [2 1], 'rows')
Z(end 1:end 4,:) = [x(1) x(1); x(2) x(1); x(2) x(2); x(1) x(2)];
end
end
Z =
0.7000 0.7000i 0.0000 0.0000i
0.7000 - 0.7000i 0.0000 0.0000i
0.0000 0.0000i 0.7000 0.7000i
0.0000 0.0000i 0.7000 - 0.7000i
0.7000 0.7000i 0.7000 0.7000i
0.7000 0.7000i 0.7000 - 0.7000i
0.7000 - 0.7000i 0.7000 - 0.7000i
0.7000 - 0.7000i 0.7000 0.7000i
CodePudding user response:
Your code is valid if you have fix length in y
, for example if each vector in y
has one value and others are zeros, or two non-zeros values ...etc.
So you can do your code for each length separately and then build the matrix Z
by combining all other matrices.