当前位置: 代码迷 >> 综合 >> Matlab中FrechetDistance方法实现---比较两条曲线的相似性,并绘制曲线
  详细解决方案

Matlab中FrechetDistance方法实现---比较两条曲线的相似性,并绘制曲线

热度:36   发布时间:2023-10-16 14:35:58.0
  1. function [cm, cSq] = DiscreteFrechetDist(P,Q,dfcn)plot(P)%绘制曲线P
    plot(Q)%绘制曲线QsP = size(P);
    sQ = size(Q);% check validity of inputs
    if sP(2)~=sQ(2)error('Curves P and Q must be of the same dimension')
    elseif sP(1)==0cm = 0;return;
    end% initialize CA to a matrix of -1s
    CA = ones(sP(1),sQ(1)).*-1;% distance function
    if nargin==2dfcn = @(u,v) sqrt(sum( (u-v).^2 )); %  @表示定义匿名函数
    end% final coupling measure value
    cm = c(sP(1),sQ(1));% obtain coupling measure via backtracking procedure
    if nargout==2cSq = zeros(sQ(1)+sP(1)+1,2);    % coupling sequenceCApad = [ones(1,sQ(1)+1)*inf; [ones(sP(1),1)*inf CA]];  % pad CAPi=sP(1)+1; Qi=sQ(1)+1; count=1;  % counting variableswhile Pi~=2 || Qi~=2% step down CA gradient[v,ix] = min([CApad(Pi-1,Qi) CApad(Pi-1,Qi-1) CApad(Pi,Qi-1)]);if ix==1cSq(count,:) = [Pi-1 Qi];Pi=Pi-1;elseif ix==2cSq(count,:) = [Pi-1 Qi-1];Pi=Pi-1; Qi=Qi-1;elseif ix==3cSq(count,:) = [Pi Qi-1];Qi=Qi-1;endcount=count+1;end% format output: remove extra zeroes, reverse order, subtract off% padding value, and add in the last pointcSq = [flipud(cSq(1:find(cSq(:,1)==0,1,'first')-1,:))-1; sP(1) sQ(1)];
    end% debug
    % assignin('base','CAw',CA)function CAij = c(i,j)% coupling search functionif CA(i,j)>-1% don't update CA in this caseCAij = CA(i,j);elseif i==1 && j==1CA(i,j) = dfcn(P(1,:),Q(1,:));     % update the CA permanentCAij = CA(i,j);                    % set the current relevant valueelseif i>1 && j==1CA(i,j) = max( c(i-1,1), dfcn(P(i,:),Q(1,:)) );CAij = CA(i,j);elseif i==1 && j>1CA(i,j) = max( c(1,j-1), dfcn(P(1,:),Q(j,:)) );CAij = CA(i,j);elseif i>1 && j>1CA(i,j) = max( min([c(i-1,j), c(i-1,j-1), c(i,j-1)]),...dfcn(P(i,:),Q(j,:)) );CAij = CA(i,j);elseCA(i,j) = inf;end
    end     % end function, cend     % end main function

    2.在命令行中输入以下语句

    >> [data1,data2]=textread('dataP.txt','%n%n',2);
    >> [data3,data4]=textread('dataQ.txt','%n%n',2);
    >> P=[data1,data2];
    >> Q=[data3,data4];
    >> DiscreteFrechetDist(P,Q)    

    3.返回值ans就是曲线的相似度,当然数值越小说明越相似。

  相关解决方案