Finding equal members in a vector
36 次查看(过去 30 天)
显示 更早的评论
Hi. My question is how can I find equal members in a vector with their indices.
For example I have this vector.
V = [ 10 13 10 20 10 95 70 13];
Thanks a lot.
1 个评论
Image Analyst
2017-12-17
This has nothing to do with the MATLAB compiler product or the Coder product, or a GUI or 3-D plots, or nearly all the tags you applied. Save yourself some time and only put the relevant tags that will help people.
回答(4 个)
jean claude
2017-12-17
find(x==10)
2 个评论
jean claude
2017-12-17
编辑:jean claude
2017-12-17
[a] = histc(x,unique(x));
t=unique(x);
c=find(a>1);
d=0;
for i=1:length(c);
d=[d find(x==t(c(i)))];
end
d(1)=[]; % d is the output
Star Strider
2017-12-17
If you only want the repeated values in the vector and their indices:
V = [ 10 13 10 20 10 95 70 13]; % Original Vector
[Vu,~,I] = unique(V(:), 'stable'); % Unique Values
Tally = accumarray(I, 1); % Count Occurrences
Idx = bsxfun(@eq, V, Vu(Tally > 1)) .* (1:length(V)); % Determine Indices Of Multiple Values Only
OutD = [Vu(Tally > 1) Idx] % Double Matrix Result
for k1 = 1:size(Idx,1)
IdxC{k1} = Idx(k1,(Idx(k1,:)>0)); % Keep Only Non-Zero Values
OutC{k1} = {OutD(k1,1) IdxC{k1}}; % Cell Array Result
end
celldisp(OutC) % View Cell Array (Optional)
OutD =
10 1 0 3 0 5 0 0 0
13 0 2 0 0 0 0 0 8
The ‘OutC’ cell array result eliminates the zeros in the ‘(2:end)’ columns of ‘OutD’.
4 个评论
Star Strider
2017-12-17
Not that I am aware of. It is necessary to accumarray (or one of the histogram functions) to count the occurrences. Using bsxfun is necessary to do the array calculations.
Jos (10584)
2017-12-17
V = [ 10 13 10 20 10 95 70 13];
[VU, ~, j] = unique(V)
P = accumarray(j, 1:numel(V), [] ,@(x) {x})
% P{k} holds all the indices where V equals VU(k)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!