Matlab:两个向量之间的欧几里得范数(或差)

时间:2018-11-19 10:25:30

标签: matlab

我想计算向量G与数组C的每一行之间的欧式距离,同时将每一行除以向量GSD中的值。我所做的似乎效率很低。我最大的开销是多少? 我可以加快速度吗?

m=1E7;
G=1E5*rand(1,8);
C=1E5*[zeros(m,1),rand(m,8)]; 
GSD=10*rand(1,8);

%I've taken the log10 of the values because G and C are very large in magnitude. 
%Don't know if it's worth it.

for i=1:m
    dG(i,1)=norm((log10(G)-log10(C(i,2:end)))/log10(GSD));
end

使用下面的示例,它们给出的答案并不完全相同。实际上,它们都没有给出相同的答案(请参见下图,使用:

dG = pdist2(log10(G),log10(C(:,2:end)),'mahalanobis',diag(log10(GSD))); %(1)

dG = sqrt(sum((log10(G)-log10(C(:,2:end))./log10(GSD)).^2,2)); 

tmp=bsxfun(@rdivide,bsxfun(@minus,log10(G),log10(C(:,2:end))),log10(GSD)); %(4)
dG = sqrt(sum(tmp.^2,2));

enter image description here

2 个答案:

答案 0 :(得分:1)

您可以使用pdist2(x,y)计算/* Copyright (c) myEd (made by students, for students.) 2018. */ body { /* General Body Properties */ font-family: Trebuchet MS, Georgia, Helvetica, sans-serif; margin: 0; overflow: hidden; } * { /* Key element to aligning <divs> (DO NOT REMOVE) */ box-sizing: border-box; } #wrapper { bottom: 0; left: 0; overflow: hidden; position: absolute; right: 0; top: 0 } .header { /* Header properties featuring myEd and school emblems */ padding: 30px; text-align: center; /* Aligns links in the navBar */ background-image: url(/Default/Img/backgroundHeader-4K.jpg); background-size: cover; background-repeat: no-repeat; } .myEd-reverse { width: 240px; height: 70px; float: left; margin-left: -60px; margin-top: -40px; } .navBar { /* Sticky navBar, either relative or fixed, depending on the scroll position of the site */ overflow: hidden; background-color: #333; position: sticky; position: -webkit-sticky; top: 0; z-index: 100000; } .navBar a { /* Style the navBar links */ float: left; display: block; color: #fff; text-align: center; padding: 6px 12px; text-decoration: none; } .navBar a.right { /* Float Quick Links, Help and Log-out navBar directory items to the right of the navBar */ float: right; } .navBar a:hover { /* Change color on hover */ background-color: #ddd; color: #000; } .navBar a.active { /* Active Link */ background-color: #666; color: #fff; } .modalDialog { /* Modal Properties for all modals in myEd */ position: fixed; font-family: Trebuchet MS, Georgia, Helvetica, sans-serif; top: 0; right: 0; bottom: 0; left: 0; background: rgba(0,0,0,0.8); z-index: 99999; opacity:0; -webkit-transition: opacity 400ms ease-in; -moz-transition: opacity 400ms ease-in; transition: opacity 400ms ease-in; pointer-events: none; } .modalDialog:target { /* Modal Properties for all modals in myEd */ opacity:1; pointer-events: auto; } .modalDialog > div { /* Modal Properties for all modals in myEd */ width: 400px; position: relative; margin: 10% auto; padding: 5px 20px 13px 20px; border-radius: 10px; background: #d7d7d7; background: -moz-linear-gradient(#ddd, #666); background: -webkit-linear-gradient(#ddd, #666); background: -o-linear-gradient(#ddd, #666); } .close { /* Modal Properties for all modals in myEd */ background: #606061; color: #FFFFFF; line-height: 10px; position: absolute; right: -5px; text-align: center; top: -10px; width: -5px; text-decoration: none; font-weight: bold; -webkit-border-radius: 12px; -moz-border-radius: 12px; border-radius: 12px; -moz-box-shadow: 1px 1px 3px #000; -webkit-box-shadow: 1px 1px 3px #000; box-shadow: 1px 1px 3px #000; } .close:hover { background: #00d9ff; } /* Modal Properties for all modals in myEd */ .textModalh2 { /* Text preferences of the modal box directed from the navBar */ margin-top: 18px; margin-bottom: -15px; } .textModalh5 { /* Text preferences of the modal box directed from the navBar */ margin-bottom: -15px; } .CARTHmodal { /* Image preferences of CARTH in the modal box directed from the navBar */ border-radius: 50%; height: 130px; width: 120px; float: left; margin-right: 40px; margin-top: 15px } .row { /* Alignment between sideBar and main */ display: -ms-flexbox; display: flex; -ms-flex-wrap: wrap; flex-wrap: wrap; } .sideBar { /* sideBar alignment properties */ -ms-flex: 20%; flex: 20%; background-color: #a7a7a7; padding: 0px; height: 5000px; /* As standard to work on all screen resolutions, overflow hides excess */ overflow-y: scroll; /* Allows myEd news to scroll */ overflow: hidden; position: relative; } #newsmyEd { position: absolute; width: 100%; height: calc(100vh - 120px); top: 0; right: 0; } .main { /* Main Column */ -ms-flex: 80%; flex: 80%; background-color: #a7a7a7; padding: 0px; height: 5000px; /* As standard to work on all screen resolutions, overflow hides excess */ overflow-y: scroll; /* Allows embedded iFrame to scroll */ position: relative; overflow: hidden; } .frameMain { /* Frame Properties */ position: absolute; top: 0; left: 0; border: 0; height: calc(100vh - 120px); } .footer { /* Footer properties */ position: fixed; bottom: 0; left: 0; right: 0; background-color: #333; } .footer a { /* Footer navBar properties */ float: right; display: block; color: #fff; text-align: center; padding: 6px 12px; text-decoration: none; } .footer a:hover { /* Change color on hover */ background-color: #ddd; color: #000; } .footer a.active { /* Active Link */ background-color: #666; color: #fff; } @media (max-width: 768px) { .sideBar { display: none !important; } }<!DOCTYPE HTML> <!-- Copyright (c) myEd (made by students, for students.) 2018. --> <html lang="en"> <head> <title>Welcome | myEd</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" type="text/css" href="/Home/Welcome/Style/style.css"> <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.4.1/css/all.css" integrity="sha384-5sAR7xN1Nv6T6+dT2mhtzEpVJvfS3NScPQTrOxhwjIuvcA67KV2R5Jz6kr4abQsz" crossorigin="anonymous"> <link rel="shortcut icon" href="/Default/Img/faviconmyEd-16x16.png"> </head> <body> <script> // Required scripts for certain activities on myEd, each entry will be noted for understanding </script> <div id="wrapper"> <!-- Wrapper fitting website to all screen resolutions --> <div class="header"> <!-- Header featuring 4K Background, myEd and school emblems --> <img src="/Default/Img/myEd-reverse.png" class="myEd-reverse" alt="myEd" title="myEd Software"> <!-- myEd Software --> </div> <div class="navBar"> <!-- Main sticky navBar with categories listed --> <a href="#" class="active" title="Home Workspace"><i class="fa fa-home"></i></a> <!-- Home --> <a href="#" title="Learning Workspace"><i class="fa fa-book"></i></a> <!-- Learning --> <a href="#" title="Student Management Workspace"><i class="fa fa-user"></i></a> <!-- Student Management --> <a href="#" title="Portal Workspace"><i class="fa fa-globe"></i></a> <!-- Portals --> <a href="#" class="right" title="Log-out"><i class="fas fa-sign-out-alt"></i></a> <!-- Log-out (right to left) --> <a href="#" class="right" title="Help"><i class="fa fa-question-circle"></i></a> <!-- Help (right to left) --> <a href="#modalQL" class="right" title="Quick Links"><i class="fa fa-bookmark"></i></a> <!-- Quick Links (right to left) --> <div id="modalQL" class="modalDialog"> <div> <a href="#close" title="Close Quick Links" class="close">X</a> <h3>Quick Links</h3> <h6><a href="https://kempsey.cs.adventist.edu.au/" target="_blank" rel="noopener" title="Click Here to be redirected to SEQTA Learn">SEQTA Learn</a></h6> <h6><a href="https://kempsey.cp.adventist.edu.au/" target="_blank" rel="noopener" title="Click Here to be redirected to SEQTA Engage">SEQTA Engage</a></h6> <h6><a href="https://www.adventist.edu.au" target="_blank" rel="noopener" title="Click Here to be redirected to ASA">Adventist Schools Australia</a></h6> <br> <h6><em>The links above are not available via the Portal Pages of myEd as they are unable to be embedded into the software.</em></h6> </div> </div> <a href="#modalPO" class="right" title="Profile Overview"><i class="fas fa-caret-down"></i></a> <!-- Profile dropDown (right to left) --> <div id="modalPO" class="modalDialog"> <div> <a href="#close" title="Close Profile Overview" class="close">X</a> <img class="CARTHmodal" src="/Default/Img/CARTH.jpg" title="Carpenter, Tom" alt="CARTH"> <h2 class="textModalh2">Tom Carpenter</h2> <h5 class="textModalh5" title="tom.carpenter@kas.nsw.edu.au">tom.carpenter@kas.nsw.edu.au</h5> <h5 class="textModalh5" title="Student ID: #714020 (CARTH)">#714020 (CARTH)</h5> <h5 class="textModalh5" title="Year Group: Year 11 (11B)">Year 11 (11B)</h5> <h5>Kempsey Adventist School</h5> </div> </div> </div> <div class="row"> <!-- Format for alignment of the sideBar and main panels --> <div class="main"> <!-- Main learning or collaborative workspace --> <iframe class="frameMain" width="100%" src="/Home/Welcome/iFrame/index.html"></iframe> <!-- Embedded frame --> </div> <div class="sideBar"> <!-- sideBar featuring Profile, myEd News and directory --> <iframe id="newsmyEd" frameborder="0" src="/Default/News/index.html"></iframe> <!-- myEd News --> </div> </div> <div class="footer"> <!-- Featuring Site Navigation and myEd News navBar links --> <a href="#" class="active" title="myEd News"><i class="far fa-newspaper"></i></a> <!-- myEd News --> <a href="/Home/Welcome/siteNavigation/index.html" title="Site Navigation"><i class="fa fa-location-arrow"></i></a> <!-- Site Navigation --> </div> </div> </body> </html>中所有元素之间的成对距离,因此您的示例将类似于

x

其中名称对ydG = pdist2(log10(G),log10(C(:,2:end)),'mahalanobis',diag(log10(GSD)).^2); 作为权重放在Eucledean上,这被称为马氏距离。

请注意,Mahalanobis距离最初用于规范化数据,因此必须将“协方差”作为第四个输入,然后MATLAB会在对角线处找到(元素对角的平方根的Cholesky分解,如下所示) )。

隐式扩展

在较新的MATLAB版本中,也可以只是隐式扩展,因为第一个条目只有一个向量。

'mahalanobis',diag(log10(GSD)).^2

这可能会快一点,但是我更喜欢log10(GSD)解决方案,因为我发现它更清晰。

答案 1 :(得分:1)

浮点应该处理大范围的输入数据,直到float数据到某个点为止,而double数据到任何合理值为止

realmax('single')
ans =
  3.4028e+38

realmax('double')
ans =
  1.7977e+308

如果1e7值在+/- 1e5范围内,则您可以预期欧几里得距离的平方在+/- 1e17范围内(5 + 5 + 7),这两种格式都可以轻松处理。 >

在任何情况下,您都应该对代码进行矢量化处理以消除循环(Matlab的处理历史非常低效,尤其是在旧版本中)

对于新版本(2016b和更高版本),只需使用:

tmp=(log10(G)-log10(C(:,2:end)))./log10(GSD);
dG = sqrt(sum(tmp.^2,2)); %row-by-row norm

请注意,您必须使用./(按元素划分),而不是/(按矩阵右划分)。

以下代码将随处可见

tmp=bsxfun(@rdivide,bsxfun(@minus,log10(G),log10(C(:,2:end))),log10(GSD));
dG = sqrt(sum(tmp.^2,2)); %row-by-row norm

但是,我认为使用log10是一个数学错误。结果dG将不是欧几里得范数。您应该坚持加权差异的均方根:

dG = sqrt(sum(bsxfun(@rdivide,bsxfun(@minus,G,C(:,2:end)),GSD).^2,2)); % all versions
dG = sqrt(sum((G-C(:,2:end)./GSD).^2,2)); %R2016b and later