Matlab 整数在向量中的位置

Matlab 整数在向量中的位置,matlab,matrix,indexing,Matlab,Matrix,Indexing,我认为这个问题很基本,但从一段时间以来我一直很忙 假设有一个向量包含4个随机重复的整数,如: v = [ 1 3 3 3 4 2 1 2 3 4 3 2 1 4 3 3 4 2 2] 我正在搜索每个整数的所有位置的向量,例如,对于1,它应该是如下所示的向量: position_one = [1 7 13] 因为我想搜索100x10000矩阵的每一行,所以我无法处理线性索引 提前谢谢 在矩阵中查找线性索引:I=Find(a==1) 在矩阵A中查找二维索引:[行,列]=Find(A==1) 以

我认为这个问题很基本,但从一段时间以来我一直很忙

假设有一个向量包含4个随机重复的整数,如:

v = [ 1 3 3 3 4 2 1 2 3 4 3 2 1 4 3 3 4 2 2] 
我正在搜索每个整数的所有位置的向量,例如,对于1,它应该是如下所示的向量:

position_one = [1 7 13]
因为我想搜索100x10000矩阵的每一行,所以我无法处理线性索引


提前谢谢

在矩阵中查找线性索引:
I=Find(a==1)

在矩阵A中查找二维索引:
[行,列]=Find(A==1)

以线性索引的形式查找:

find(A == 1)

ans =

     2
    10
    14

%This is the same as reshaping A to a vector and find ones in the vector:
B = A(:);
find(B == 1);

B' =

     0     1     0     0     0     0     0     0     0     1     0     0     0     1     0     0     0     0     0     0
查找二维索引:

[row, col] = find(A == 1)

row =

     2
     5
     4


col =

     1
     2
     3

行和列

由于每个整数的输出都会更改,因此单元格数组将适合整个任务。对于整个矩阵,您可以执行以下操作:

A = randi(4,10,30); % some data
Row = repmat((1:size(A,1)).',1,size(A,2)); % index of the row
Col = repmat((1:size(A,2)),size(A,1),1); % index of the column
pos = @(n) [Row(A==n) Col(A==n)]; % Anonymous function to find the indices of 'n'
对于每个
n
,您可以编写:

>> pos(3)
ans =
     1     1
     2     1
     5     1
     6     1
     9     1
     8     2
     3     3
     .     .
     .     .
     .     .
其中第一列是行,第二列是
A
n
的每个实例的列

对于所有
n
s,您可以使用
arrayfun

positions = arrayfun(pos,1:max(A(:)),'UniformOutput',false) % a loop that goes over all n's
或一个简单的
循环(更快):

这两种情况下的输出都是单元格数组:

positions = 
    [70x2 double]    [78x2 double]    [76x2 double]    [76x2 double]
对于每个
n
,您可以编写
位置{n}
,以获得例如:

>> positions{1}
ans =
    10     1
     2     3
     5     3
     3     4
     5     4
     1     5
     4     5
     .     .
     .     .
     .     .
仅行

如果您希望列索引中的每一行和
n
,您可以这样写:

A = randi(4,10,30);
row_pos = @(k,n) A(k,:)==n;
positions = false(size(A,1),max(A(:)),size(A,2));
for n = 1:max(A(:))
    positions(:,n,:) = row_pos(1:size(A,1),n);
end
现在,
positions
是一个逻辑三维数组,每一行对应于
a
中的一行,每一列对应于
n
的值,第三维是行和
n
组合的存在向量。这样,我们可以将
R
定义为列索引:

R = 1:size(A,2);
然后找到给定行和
n
的相关位置。例如,第9行中
n=3
的列索引为:

>> R(positions(9,3,:))
ans =
     2     6    18    19    23    24    26    27
这就像调用
find(A(9,:)==3)
,但是如果您需要多次执行此操作,那么查找所有索引并将其存储在
位置(这是合乎逻辑的,所以不会太大)会更快

positions = accumarray(v(:), 1:numel(v), [], @(x) {sort(x.')});
为了

这给

positions{1} =
     1     7    13
positions{2} =
     6     8    12    18    19
positions{3} =
     2     3     4     9    11    15    16
positions{4} =
     5    10    14    17

position\u one=find(v==1)
会为您做这项工作吗,或者您需要更复杂的东西吗?find的问题是sub2in与线性索引有问题,我不知道为什么;)但是我会尝试第二个答案,因为我已经开始做类似的事情了……谢谢你的帮助!
positions = accumarray(v(:), 1:numel(v), [], @(x) {sort(x.')});
v = [ 1 3 3 3 4 2 1 2 3 4 3 2 1 4 3 3 4 2 2];
positions{1} =
     1     7    13
positions{2} =
     6     8    12    18    19
positions{3} =
     2     3     4     9    11    15    16
positions{4} =
     5    10    14    17