Pairwise difference between values of a vector

14 ビュー (過去 30 日間)
Itai
Itai 2012 年 7 月 29 日
回答済み: James Cai 2018 年 1 月 31 日
Hello all, I want to calculate the mean of all absolute pairwise differences (Ei-Ej) from a long set of values. for example (3 1 2) the differences are 3-1=2, 3-2=1, 1-2=1 and the mean (2+1+1)/3=1.33. Thanks for helping

採用された回答

the cyclist
the cyclist 2012 年 7 月 29 日
One way:
% The data
v = [3 1 2];
% Number of elements
nv = numel(v);
% Absolute pairwise diifferences
dv = abs(bsxfun(@minus,v,v'));
% Sum the differences (This double-counts, but we'll double-count the denominator, too)
sdtv = sum(dv(:));
% Number pairs (also double-counted)
np = nv^2 - nv;
% The mean
mdtv = sdtv/np

その他の回答 (1 件)

James Cai
James Cai 2018 年 1 月 31 日
Nowadays, dv = abs(v-v') returns the absolute pairwise differences.

カテゴリ

Help Center および File ExchangeDevelop Apps Using App Designer についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by