unique pairs - frequency appearance

3 ビュー (過去 30 日間)
Beginner22 may
Beginner22 may 2015 年 7 月 20 日
回答済み: Guillaume 2015 年 7 月 20 日
Hello, how can I make a table of unique pairs frequency appearance according to a specific column (In the example - Column A)? example in the image:
10 & 10 - mark +1 in the correct cell in the table.
10& 30 - mark +1 in the correct cell in the table
later in "2":
10&20 - mark + in the correct cell in the table.
et cetera
Can you help me bulid the algoritem for a table like this? Thank you!

回答 (1 件)

Guillaume
Guillaume 2015 年 7 月 20 日
There are two parts to your question. First, the hard bit is going from your table to a list of pairs. The second bit, much easier, is building the histograms of the pair.
One possible way to build the list of pairs:
data = [1 10;1 10;1 30;2 10;2 20; 3 30;3 20;3 10;4 10;4 20;4 30]
%the accumarray below assumes that column 1 is always integer positive from 1.
%column 1 does not need to be ordered:
groupeddata = accumarray(data(:, 1), data(:, 2), [], @(v) {v});
%nchoosek to generate pairs, sort(..., 2) to remove the ordering, unique(...) to remove duplicates:
pairpergroup = cellfun(@(v) unique(sort(nchoosek(v, 2), 2), 'rows'), groupeddata, 'UniformOutput', false);
allpairs = vertcat(pairpergroup{:})
One possible way to build the histogram:
[pair, ~, row] = unique(allpairs, 'rows');
pairdist = [pair, histcounts(row, [unique(row); Inf])']

カテゴリ

Help Center および File ExchangeTables についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by