3

我们有 pei = 1:25迭代。每个迭代结果都是一个1xlength(N)元胞数组,其中0<=N<=25.

iteration 1: 4     5     9    10     20     
iteration 2: 3     8     9    13     14      6
...
iteration 25:  1     2    3

我们将所有迭代的结果评估为一个根据频率排序的矩阵,每个值按降序重复,如下例所示:

Matrix=
  Columns 1 through 13
    16    22    19    25     2     5     8    14    17    21     3    12    13
     6     5     4     4     3     3     3     3     3     3     2     2     2
  Columns 14 through 23
    18    20     1     6     7     9    10    11    15    23
     2     2     1     1     1     1     1     1     1     1

结果说明:第 1 列:N == 16出现在 6 次迭代中,第 2 列:N == 22出现在 5 次迭代中,等等。如果在任何迭代N中都没有显示数字(在该范例N == 4N == 24),则也不会以频率索引为零列出。

我想将每个迭代 ( i) 与第一个迭代相关联,N它显示 peN == 9仅在第一次迭代中出现i = 1,而不出现在i = 2太中,N == 3只出现在i = 2而不出现在i = 25太等,直到所有i' 都唯一关联到N'。

先感谢您。

4

2 回答 2

2

这是一种使用uniqueR2012a 中引入的功能(即,它将索引返回到第一个值)的方法

%# make some sample data
iteration{1} = [1 2 4 6];
iteration{2} = [1 3 6];
iteration{3} = [1 2 3 4 5 6];
nIter= length(iteration);

%# create an index vector so we can associate N's with iterations
nn = cellfun(@numel,iteration);
idx = zeros(1,sum(nn));
idx([1,cumsum(nn(1:end-1))+1]) = 1;
idx = cumsum(idx); %# has 4 ones, 3 twos, 6 threes

%# create a vector of the same length as idx with all the N's
nVec = cat(2,iteration{:});

%# run `unique` on the vector to identify the first occurrence of each N
[~,firstIdx] = unique(nVec,'first');

%# create a "cleanIteration" array, where each N only appears once
cleanIter = accumarray(idx(firstIdx)',firstIdx',[nIter,1],@(x){sort(nVec(x))},{});

cleanIter = 
    [1x4 double]
    [         3]
    [         5]

>> cleanIter{1}
ans =
     1     2     4     6
于 2012-11-26T16:42:58.637 回答
1

这是另一个使用accumarray. 评论中的解释

% example data (from your question)
iteration{1} = [4     5     9    10     20  ];
iteration{2} = [3     8     9    13     14      6];
iteration{3} = [1     2    3];
niterations = length(iteration);

% create iteration numbers 
% same as Jonas did in the first part of his code, but using a short loop
for i=1:niterations
    idx{i} = i*ones(size(iteration{i}));
end

% count occurences of values from all iterations
% sort them in descending order
occurences = accumarray([iteration{:}]', 1);
[occ val] = sort(occurences, 1, 'descend');

% remove zero occurences and create the Matrix
nonzero = find(occ);
Matrix = [val(nonzero) occ(nonzero)]'

Matrix =

 3     9     1     2     4     5     6     8    10    13    14    20
 2     2     1     1     1     1     1     1     1     1     1     1


% find minimum iteration number for all occurences
% again, using accumarray with @min function
assoc = accumarray([iteration{:}]', [idx{:}]', [], @min);
nonzero = find(assoc);
result = [nonzero assoc(nonzero)]'

result =

 1     2     3     4     5     6     8     9    10    13    14    20
 3     3     2     1     1     2     2     1     1     2     2     1
于 2012-11-26T19:44:16.710 回答