从原始矩阵中找到向量的指数

时间:2021-07-26 04:18:18

I have a matrix of 2d lets assume the values of the matrix

我有一个二维矩阵假设这个矩阵的值

a =
    17    24     1     8    15
    23     5     7    14    16
     4     6    13    20    22
    10    12    19    21     3
    17    24     1     8    15
    11    18    25     2     9

This matrix is going to be divided into three different matrices randomly let say

这个矩阵将被随机分为三个不同的矩阵

b =
     17    24     1     8    15
     23     5     7    14    16

c =
      4     6    13    20    22
     11    18    25     2     9

d =
     10    12    19    21     3
     17    24     1     8    15

How can i know the index of the vectors in matrix d for example in the original matrix a,note that the values of the matrix can be duplicated. for example if i want to know the index of {10 12 19 21 3} in matrix a? or the index of {17 24 1 8 15} in matrix a,but for this one should return only on index value? I would appreciate it so much if you can help me with this. Thank you in advance

我如何知道矩阵d中的向量的指数,例如在原始矩阵a中,注意到矩阵的值可以被复制。例如,如果我想知道矩阵a中的{10 12 19 21 3}的索引?或者矩阵a中{17 24 1 8 15}的索引,但是对于这个索引,应该只返回索引值?如果你能帮我做这件事,我会非常感激。提前谢谢你

2 个解决方案

#1


3  

You can use ismember with the 'rows' option. For example:

您可以使用ismember与“row”选项。例如:

tf = ismember(a, c, 'rows')

Should produce:

应该生产:

tf =
     0
     0
     1
     0
     0
     1

To get the indices of the rows, you can apply find on the result of ismember (note that it's redundant if you're planning to use this vector for matrix indexing). Here find(tf) return the vector [3; 6].

要获得行索引,可以对ismember的结果应用find(注意,如果计划使用这个向量进行矩阵索引,那么它是多余的)。这里找到(tf)返回向量[3;6)。

If you want to know the number of the row in matrix a that matches a single vector, you either use the method explained and apply find, or use the second output parameter of ismember. For example:

如果想知道矩阵a中与单个向量匹配的行数,可以使用解释和应用查找的方法,或者使用ismember的第二个输出参数。例如:

[tf, loc] = ismember(a, [10 12 19 21 3], 'rows')

returns loc = 4 for your example. Note that here a is the second parameter, so that the output variable loc would hold a meaningful result.

为您的示例返回loc = 4。注意,这里a是第二个参数,因此输出变量loc将保存有意义的结果。

Handling floating-point numbers

If your data contains floating point numbers, The ismember approach is going to fail because floating-point comparisons are inaccurate. Here's a shorter variant of Amro's solution:

如果您的数据包含浮点数,ismember方法将会失败,因为浮点比较是不准确的。下面是Amro银行解决方案的一个较短的变体:

x = reshape(c', size(c, 2), 1, []);
tf = any(all(abs(bsxfun(@minus, a', x)) < eps), 3)';

Essentially this is a one-liner, but I've split it into two commands for clarity:

本质上这是一行代码,但为了清晰起见,我将它分为两个命令:

  • x is the target rows to be searched, concatenated along the third dimension.
  • x是要搜索的目标行,并沿着第三维连接。
  • bsxfun subtracts each row in turn from all rows of a, and the magnitude of the result is compared to some small threshold value (e.g eps). If all elements in a row fall below it, mark this row as "1".
  • bsxfun从a的所有行依次减去每一行,结果的大小与某个小阈值(e)进行比较。g eps)。如果一行中的所有元素都低于它,那么将这一行标记为“1”。

#2


1  

It depends on how you build those divided matrices. For example:

这取决于你如何构建这些划分矩阵。例如:

a = magic(5);
d = a([2 1 2 3],:);

then the matching rows are obviously: 2 1 2 3

那么匹配的行显然是:2 1 2 3


EDIT:

Let me expand on the idea of using ismember shown by @EitanT to handle floating-point comparisons:

让我扩展一下使用@EitanT显示的ismember来处理浮点比较的想法:

tf = any(cell2mat(arrayfun(@(i) all(abs(bsxfun(@minus, a, d(i,:)))<1e-9,2), ...
    1:size(d,1), 'UniformOutput',false)), 2)

not pretty but works :) This would be necessary for comparisons such as: 0.1*3 == 0.3

这对于比较是必要的,例如:0.1*3 = 0.3

(basically it compares each row of d against all rows of a using an absolute difference)

(基本上它用绝对差来比较d的每一行和a的所有行)

#1


3  

You can use ismember with the 'rows' option. For example:

您可以使用ismember与“row”选项。例如:

tf = ismember(a, c, 'rows')

Should produce:

应该生产:

tf =
     0
     0
     1
     0
     0
     1

To get the indices of the rows, you can apply find on the result of ismember (note that it's redundant if you're planning to use this vector for matrix indexing). Here find(tf) return the vector [3; 6].

要获得行索引,可以对ismember的结果应用find(注意,如果计划使用这个向量进行矩阵索引,那么它是多余的)。这里找到(tf)返回向量[3;6)。

If you want to know the number of the row in matrix a that matches a single vector, you either use the method explained and apply find, or use the second output parameter of ismember. For example:

如果想知道矩阵a中与单个向量匹配的行数,可以使用解释和应用查找的方法,或者使用ismember的第二个输出参数。例如:

[tf, loc] = ismember(a, [10 12 19 21 3], 'rows')

returns loc = 4 for your example. Note that here a is the second parameter, so that the output variable loc would hold a meaningful result.

为您的示例返回loc = 4。注意,这里a是第二个参数,因此输出变量loc将保存有意义的结果。

Handling floating-point numbers

If your data contains floating point numbers, The ismember approach is going to fail because floating-point comparisons are inaccurate. Here's a shorter variant of Amro's solution:

如果您的数据包含浮点数,ismember方法将会失败,因为浮点比较是不准确的。下面是Amro银行解决方案的一个较短的变体:

x = reshape(c', size(c, 2), 1, []);
tf = any(all(abs(bsxfun(@minus, a', x)) < eps), 3)';

Essentially this is a one-liner, but I've split it into two commands for clarity:

本质上这是一行代码,但为了清晰起见,我将它分为两个命令:

  • x is the target rows to be searched, concatenated along the third dimension.
  • x是要搜索的目标行,并沿着第三维连接。
  • bsxfun subtracts each row in turn from all rows of a, and the magnitude of the result is compared to some small threshold value (e.g eps). If all elements in a row fall below it, mark this row as "1".
  • bsxfun从a的所有行依次减去每一行,结果的大小与某个小阈值(e)进行比较。g eps)。如果一行中的所有元素都低于它,那么将这一行标记为“1”。

#2


1  

It depends on how you build those divided matrices. For example:

这取决于你如何构建这些划分矩阵。例如:

a = magic(5);
d = a([2 1 2 3],:);

then the matching rows are obviously: 2 1 2 3

那么匹配的行显然是:2 1 2 3


EDIT:

Let me expand on the idea of using ismember shown by @EitanT to handle floating-point comparisons:

让我扩展一下使用@EitanT显示的ismember来处理浮点比较的想法:

tf = any(cell2mat(arrayfun(@(i) all(abs(bsxfun(@minus, a, d(i,:)))<1e-9,2), ...
    1:size(d,1), 'UniformOutput',false)), 2)

not pretty but works :) This would be necessary for comparisons such as: 0.1*3 == 0.3

这对于比较是必要的,例如:0.1*3 = 0.3

(basically it compares each row of d against all rows of a using an absolute difference)

(基本上它用绝对差来比较d的每一行和a的所有行)