简体   繁体   English

如何在 matlab 中找到具有这种组织的两个向量之间的所有可能交集

[英]how to find all the possible intersections between two vectors with such organization in matlab

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 .我有一个矩阵y和一个向量x ,我需要找到将x中的每个值映射到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];让我们用一个例子来解释一下:这是一个例子,我有向量x = [0.7 + 0.7i; 0.7-0.7i]; x = [0.7 + 0.7i; 0.7-0.7i]; The matrix y = [1 0; 2 0; 1 2];矩阵y = [1 0; 2 0; 1 2]; 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];结果矩阵应该是这样的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]; 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];这相当于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]; 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 .这意味着它 map 根据y中的索引值将x中的每个值放入Z的行。

Here is my try code: clear all;这是我的尝试代码:全部清除; clc; cl;

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 .我的代码中矩阵Z显示逆矩阵的问题,这意味着它从x获取输入x_1 ,然后将 map 输入到y中的所有可能值中。 For example the matrix Z starts with [x_1 0; 0 x_1; x_1 x_1 ….]例如矩阵Z[x_1 0; 0 x_1; x_1 x_1 ….] [x_1 0; 0 x_1; x_1 x_1 ….] [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 …..] [x_1 0; 0 x_1; x_1 x_1 ….] ,但这应该是相反的,这意味着取x和 map 中的每个值,如上例所示[x_1 0; x_2 0; x_3 0 …..] [x_1 0; x_2 0; x_3 0 …..] [x_1 0; x_2 0; x_3 0 …..] . [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];第二个问题,当y包含多个非零值时,我的代码无法获取所有可能的向量,它只能获取[x_1 x_1; x_2 x_2]; [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]但我不能 map 其他可能性是[x_1 x_2; x_2 x_1] [x_1 x_2; x_2 x_1] and so on. [x_1 x_2; x_2 x_1]等等。

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 .我有向量x和矩阵y ,我需要按照从矩阵y中的每一行获取的索引填充矩阵z For example, if the first row in matrix y is [1 0] or [0 1] ;例如,如果矩阵y的第一行是[1 0][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.然后我将从 x 中获取所有可能的值,并将其放入z中,然后是从y中的行中获取的数字,在这种情况下为 1。 Then, the same case for row 2 in matrix y which is [2 0] or [0 2] ;然后,矩阵y中的第 2 行的情况相同,即[2 0][0 2] it means that second column in z will be filled with all possible values in x.这意味着 z 中的第二列将填充 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.然后,可以填充z中的两列,这等效于y中的情况[1 2] ,因此它将从x中获取第一个值并用x中的所有其他可能值填充它,依此类推。 The rows in z should not be repeated. z中的行不应重复。
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.矩阵Z与下面 AboAmmar 的答案完全相同,但是如果使用更长的向量x和更大的矩阵y的循环会有点复杂。

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.如果您在y中具有固定长度,则您的代码是有效的,例如,如果y中的每个向量都有一个值,而其他向量是零,或者两个非零值......等等。

So you can do your code for each length separately and then build the matrix Z by combining all other matrices.因此,您可以分别为每个长度编写代码,然后通过组合所有其他矩阵来构建矩阵Z

As you describe it, there are 4 distinct cases for each row of y and the corresponding output:正如您所描述的, y的每一行和相应的 output 有 4 种不同的情况:

  1. [0 1] or [1 0] => [ x 0 ] [0 1][1 0] => [ x 0 ]
  2. [0 2] or [2 0] => [ 0 x ] [0 2][2 0] => [ 0 x ]
  3. [1 2] => [x1 x1; [1 2] => [x1 x1; x1 x2; x1 x2; x2 x2; x2 x2; x2 x1] x2 x1]
  4. [2 1] => [x1 x1; [2 1] => [x1 x1; x2 x1; x2 x1; x2 x2; x2 x2; x1 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.因此,最简单(但不是最聪明)的解决方案是使用if-else和 select 上述合适的情况。 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;我们没有关于可能索引的所有信息,或者是否可能出现[1 1][2 2]之类的行,因此以下解决方案绝不是详尽无遗的; surprising errors might happen if other inputs are fed into y matrix.如果将其他输入输入y矩阵,则可能会出现令人惊讶的错误。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM