unique pairs - frequency appearance

2 views (last 30 days)
Beginner22 may
Beginner22 may on 20 Jul 2015
Answered: Guillaume on 20 Jul 2015
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!

Answers (1)

Guillaume
Guillaume on 20 Jul 2015
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])']

Categories

Find more on Tables in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!