Optimize code: find longest common sequence of two strings
이전 댓글 표시
Hi all! I have a problem: I create this code to compare strings in Trajectory.mat (attached) and find the longest common sequence. I have used the function at this link http://www.mathworks.com/matlabcentral/fileexchange/24559-longest-common-subsequence
% delete cell of Trajctory that are empty
empties = find(cellfun(@isempty,Trajectory));
Trajectory(empties) = [];
% Compare strings in Trajectory: find LCS (Longest Common Sequence)
[D, dist, aLongestString] = LCS(Trajectory{1,1},Trajectory{2,1});
% Count patterns in LCS
LCS=size(aLongestString,2);
% Count patterns in strings that I compare
Q=size(Trajectory{2,1},2);
P=size(Trajectory{1,1},2);
% Partecipation ratio of the common part to a pattern P
RatioQ = LCS./Q;
RatioP = LCS./P;
% MSTP-Similarity Equal Aveage
EA=(RatioP + RatioQ)/2;
% MSTP-Similarity Weighted Aveage
WA=(P*RatioP+Q*ratioQ)/(P+Q);
The code works right but I want to optimize that: I want to compare all the strings in Trajectory because with this code I have to write a lot of times the same code for every two strings. Can you give me some suggestion to optimize the code and compare all the strings in Trajectory? I try to use a for loop but with disastrous results.
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!