Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用Matlab求矩阵中相等元素的位置_Matlab_Matrix - Fatal编程技术网

用Matlab求矩阵中相等元素的位置

用Matlab求矩阵中相等元素的位置,matlab,matrix,Matlab,Matrix,假设我有: m = [1,2,3;1,4,5;6,4,7] 我想得到一个列表,其中包含矩阵m中元素的位置,以便将相等元素的位置分组在一起。矩阵m的输出必须为: 我们可以在这里看到,所有相等元素的位置都被分组在一起。最简单的方法是循环遍历每个唯一的值,并确定与每个值匹配的行和列位置。类似这样的方法可能会奏效: val = unique(m); pos = cell(1, numel(val)); for ii = 1 : numel(val) [r,c] = find(m == val(

假设我有:

m = [1,2,3;1,4,5;6,4,7]
我想得到一个列表,其中包含矩阵m中元素的位置,以便将相等元素的位置分组在一起。矩阵m的输出必须为:


我们可以在这里看到,所有相等元素的位置都被分组在一起。

最简单的方法是循环遍历每个唯一的值,并确定与每个值匹配的行和列位置。类似这样的方法可能会奏效:

val = unique(m);
pos = cell(1, numel(val));
for ii = 1 : numel(val)
    [r,c] = find(m == val(ii));
    pos{ii} = [r,c];
end
pos是一个单元格数组,包含每个唯一值的所有位置。我们可以通过以下方式显示这些位置:

>> format compact; celldisp(pos)
pos{1} =
     1     1
     2     1
pos{2} =
     1     2
pos{3} =
     1     3
pos{4} =
     2     2
     3     2
pos{5} =
     2     3
pos{6} =
     3     1
pos{7} =
     3     3
这当然是没有意义的,除非您特别显示每组位置的每个唯一值。因此,我们可以尝试这样的方法,在单元格数组中循环每个元素,并显示每组位置所属的对应元素:

 for ii = 1 : numel(val)
     fprintf('Value: %f\n', val(ii));
     fprintf('Positions:\n');
     disp(pos{ii});
 end
我现在得到的是:

Value: 1.000000
Positions:
     1     1
     2     1
Value: 2.000000
Positions:
     1     2
Value: 3.000000
Positions:
     1     3
Value: 4.000000
Positions:
     2     2
     3     2
Value: 5.000000
Positions:
     2     3
Value: 6.000000
Positions:
     3     1
Value: 7.000000
Positions:
     3     3

最简单的方法是循环遍历每个唯一的值,并确定与每个值匹配的行和列位置。类似这样的方法可能会奏效:

val = unique(m);
pos = cell(1, numel(val));
for ii = 1 : numel(val)
    [r,c] = find(m == val(ii));
    pos{ii} = [r,c];
end
pos是一个单元格数组,包含每个唯一值的所有位置。我们可以通过以下方式显示这些位置:

>> format compact; celldisp(pos)
pos{1} =
     1     1
     2     1
pos{2} =
     1     2
pos{3} =
     1     3
pos{4} =
     2     2
     3     2
pos{5} =
     2     3
pos{6} =
     3     1
pos{7} =
     3     3
这当然是没有意义的,除非您特别显示每组位置的每个唯一值。因此,我们可以尝试这样的方法,在单元格数组中循环每个元素,并显示每组位置所属的对应元素:

 for ii = 1 : numel(val)
     fprintf('Value: %f\n', val(ii));
     fprintf('Positions:\n');
     disp(pos{ii});
 end
我现在得到的是:

Value: 1.000000
Positions:
     1     1
     2     1
Value: 2.000000
Positions:
     1     2
Value: 3.000000
Positions:
     1     3
Value: 4.000000
Positions:
     2     2
     3     2
Value: 5.000000
Positions:
     2     3
Value: 6.000000
Positions:
     3     1
Value: 7.000000
Positions:
     3     3

这将为您提供所需的内容,但唯一元素的索引也会在单元格中包装两次,就像重复元素的索引一样:

m = [1,2,3;1,4,5;6,4,7];

[~, idx] = ismember(m(:), unique(m(:)));
linInd = 1:numel(m);
[i,j] = ind2sub(size(m), linInd);
res = accumarray(idx, linInd, [], @(x) {num2cell([i(x);j(x)]',2)});
结果:

>> celldisp(res)

res{1}{1} =
     2     1
res{1}{2} =
     1     1
res{2}{1} =
     1     2
res{3}{1} =
     1     3
res{4}{1} =
     2     2
res{4}{2} =
     3     2
res{5}{1} =
     2     3
res{6}{1} =
     3     1
res{7}{1} =
     3     3

这将为您提供所需的内容,但唯一元素的索引也会在单元格中包装两次,就像重复元素的索引一样:

m = [1,2,3;1,4,5;6,4,7];

[~, idx] = ismember(m(:), unique(m(:)));
linInd = 1:numel(m);
[i,j] = ind2sub(size(m), linInd);
res = accumarray(idx, linInd, [], @(x) {num2cell([i(x);j(x)]',2)});
结果:

>> celldisp(res)

res{1}{1} =
     2     1
res{1}{2} =
     1     1
res{2}{1} =
     1     2
res{3}{1} =
     1     3
res{4}{1} =
     2     2
res{4}{2} =
     3     2
res{5}{1} =
     2     3
res{6}{1} =
     3     1
res{7}{1} =
     3     3

我们的答案有帮助吗?我们的答案有帮助吗?