1. 示例

Input = [1,2,1,2,3]; % 1,3索引对应都为‘1’;2,4索引对应都为‘1’
Output = same_index(Input)

在这里插入图片描述

2. 函数

function Output = same_index(Input)
% Input = [1,2,1,2,3];
% Output = {[1;3]},{[2;4]}
% 注:Input的输入形式为一行多列或是多行一列的矩阵

[~, ~, ib] = unique(Input);
c = accumarray(ib, (1:numel(Input))', [], @cellhorzcat);
index = zeros(numel(c),1); % 避免使用(end+1)
n = 0;
for i = 1:numel(c)
    if numel(c{i,1})>1
        n = n+1;
        index(n,:) = i;
    end
end
index(index==0,:) = [];
Output = c(index,:);

参考:https://www.zhihu.com/question/496060981

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐