File: ef230-2023-08/www/examples/socks.m Download
% probability of choosing two matching socks from a drawer full of oddly colored socks
% note that this is too slow for 1000's of socks - if you have that many throw some away:)
% lazydog@utk.edu 11/5/2015
clear all; clc;
r=4; p=5; y=2;  % quantities of socks (test data)
list = [ ones(1,r) 2*ones(1,y) 3*ones(1,p) ]; % make a list of numbers representing the socks
combos = nchoosek(list,2);  % generate all possible combinations of choosing 2
matchcnt = nnz(combos(:,1)==combos(:,2)) ; % find the number of matching pairs
[numrows, numcols] = size(combos);
prob = matchcnt / numrows