从原始矩阵中找到向量的索引 [英] Finding index of vector from its original matrix

查看:240
本文介绍了从原始矩阵中找到向量的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个2d的矩阵,让我们假设该矩阵的值

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

我怎么知道矩阵d中向量的索引,例如在原始矩阵a中,请注意矩阵的值可以重复. 例如,如果我想知道矩阵a中的{10 12 19 21 3}的索引? 或矩阵a中的{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

推荐答案

您可以将ismember'rows'选项一起使用.例如:

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

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

应该产生:

tf =
     0
     0
     1
     0
     0
     1

要获取行的索引,可以对ismember的结果应用find(请注意,如果您打算将此向量用于矩阵索引,则是多余的).在这里find(tf)返回向量[3; 6].

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].

如果您想知道矩阵a中与单个向量匹配的行号,则可以使用说明的方法并应用find,或者使用第二个输出参数ismember.例如:

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:

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

返回示例的loc = 4.请注意,这里a是第二个参数,因此输出变量loc将具有有意义的结果.

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.

如果您的数据包含浮点数,则ismember方法将失败,因为浮点比较不准确.这是 Amro 的解决方案的较短变体:

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:

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是要搜索的目标行,沿着第三维连接.
  • bsxfuna的所有行中依次减去每一行,并将结果的大小与某个较小的阈值进行比较( eg
  • x is the target rows to be searched, concatenated along the third dimension.
  • 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".

这篇关于从原始矩阵中找到向量的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆