放在单元阵列相等的元素 [英] Place equal elements in cell array
本文介绍了放在单元阵列相等的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数组。我整理了,所以我已经整理阵列和阵列初始按排序元素的indeces。
I have an array. I sorted it, so I have sorted array and indeces of sorted elements in the initial array.
佛例如,从 [4 5 4 4 4 4 5 4]
我 [4 4 4 4 4 4 5 5]
和 [1 3 4 5 6 8 2 7]
。
如何放置收到的indeces在一个单元阵列,使得在一个小区将等于元件的indeces?在我的例子,这将是: {1 3 4 5 6 8}
, {2 7}
How to place recieved indeces in a cell array, so that in one cell will be indeces of equal elements? For my example, it will be: {1 3 4 5 6 8}
, {2 7}
.
我在寻找非循环的方式来解决这个问题。
I'm searching for non-loop way to solve it.
推荐答案
使用的 accumarray
:
x = [4 5 4 4 4 4 5 4]; %// data
[~, ~, jj] = unique(x);
result = accumarray(jj(:), 1:numel(x), [], @(v) {v(:).'});
或者,如果你需要每一套指标进行排序:
Or, if you need each set of indices sorted:
result = accumarray(jj(:), 1:numel(x), [], @(v) {sort(v(:)).'});
这篇关于放在单元阵列相等的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文