To remove the repeated number
1 次查看(过去 30 天)
显示 更早的评论
i have a matrix [1 2 2 2 3 4 5] and i want to get the result has [1 2 3 4] by replacing the repeated 2 by the single number.
回答(3 个)
Andrei Bobrov
2013-2-7
% without sorting array
m = randi(5,1,10);
[~,ii] = sort(m); % for Jan's solution
jj = [true,diff(m(ii))~=0];
out1 = m(sort(ii(jj)));
out2 = unique(m,'stable'); % in R2012a and later
[u,b] = unique(m,'first'); % old releases
[~,ii] = sort(b);
out3 = u(ii);
1 个评论
Jan
2013-2-7
No sorting, considering only neighboring elements such that e.g. [1,2,1] is not altered:
m = [1 2 2 2 3 4 5];
u = m([true, diff(m) ~= 0]);
You see, there are many different solutions, because your problem is not defined exactly: Sorting, not neighboring repetitions, ...
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!