在矩阵中查找一组元素的位置 - Matlab

时间:2012-03-02 16:04:33

标签: matlab

我有一个矩阵A. 我想找到所有独特的元素是A所以: b =唯一(A);将给出A中所有独特元素的数组。

我想在A中找到这些元素的位置。准确地说,b中的元素 在A中重复自己,我想在b中为每个元素找到它的

如果没有循环,怎么能做到这一点?

2 个答案:

答案 0 :(得分:1)

命令

[b,m,n] = unique(A);

应该为您提供回答问题所需的所有数据。

答案 1 :(得分:0)

以下是一些示例代码,它执行我认为您尝试执行的操作:

%Test data
A = [...
    1 2 3 4; ...
    4 5 6 7; ...
    8 1 3 4];

%Basic "unique" call
[B, ix_A, ix_B] = unique(A);

%Note that the indexes from unique can be used as follows
isequal(A(ix_A), B )   %Returns true
isequal(B(ix_B), A(:) )   %Returns true

%To find a row (and column) in A where each element in B can be found we
%just need to convert the linear indexs into row/column subscripts
[row, column] = ind2sub(size(A), ix_A);
%     Note that in general, multiple rows will contain each value from A.
%     This will always produce one of the rows (and columns), pracitcially,
%     it looks like to returns the last row containing the value.