【问题标题】:How to find row indexes of elements which have same value in a matrix in MATLAB?如何在MATLAB中找到矩阵中具有相同值的元素的行索引?
【发布时间】:2019-01-26 17:48:30
【问题描述】:

我有两个 4x1 矩阵。它们具有相同的元素,但矩阵中元素的行索引不同。

a = [200;100;100;300]
b = [100;100;200;300]

我需要在矩阵 a 中找到矩阵 b 的元素的索引号。 比如矩阵b的第三个元素是200,矩阵a中200的索引号是1。

结果应该是 = [2 3 1 4]

我写了这段代码,但它不起作用,因为有两个 100:

for i=1:4
    c(1,i) = find(a(:,1) == b(i,1));
end

我有这个警告:Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.

我应该怎么做才能拥有[2 3 1 4]

【问题讨论】:

    标签: matlab matrix find row


    【解决方案1】:

    您可以 sort ab 并使用排序数组的索引来获得所需的结果:

    [~,s1] = sort(a);
    [~,s2] = sort(b);
    c(s2) = s1;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-30
      • 1970-01-01
      • 1970-01-01
      • 2020-12-06
      • 1970-01-01
      相关资源
      最近更新 更多