Longest common subsequence string LCS

조회 수: 11 (최근 30일)
Alaa
Alaa 2011년 6월 3일
[EDIT: 20110603 09:53 CDT - reformat - WDR]
can some one help correct this matlab code for the recursive Longest common subsequence string LCS, the error "Subscripted assignment dimension" when run the following code
the function rec read two string from files the call the temp to fill the LCS table recursivly
function rec()
%to read the two strings from files
%x1='xyxxzxyzxy';
%x2='zxzyyzxxyxxz';
fid1=fopen('file1.txt');
fid2=fopen('file2.txt');
chs1 = textscan(fid1, '%c');
chs2 = textscan(fid2, '%c');
fclose(fid1);
fclose(fid2);
global x1;
global x2;
x1 =char(chs1);
x2 =char(chs2);
%to read the two strings from files
%creat a global table then call temp func to fill in
lenx1= length(x1)+1;
lenx2= length(x2)+1;
global L;
L = zeros(lenx1,lenx2);
temp (lenx1,lenx2);
%disp (L);
end
function result = temp (m,n)
global x1;
global x2;
global L;
result = L;
if m==1 || n==1
result(m,n) = 0;
elseif x1(m-1) == x2(n-1)
disp(['if=' x1(m-1)]);
disp(['if=' x2(n-1)]);
result(m,n) = temp(m-1,n-1) + 1;
else
disp(['else=' x1(m-1)]);
disp(['else=' x2(n-1)]);
result= max(temp(m,n-1), temp(m-1,n));
end
%L(m,n) = result(m,n);
return
end
  댓글 수: 3
Alaa
Alaa 2011년 6월 3일
you can add any two strings...the error occure when the function temp return from the base state "if m==1||n==1" to start filling the table L
Alaa
Alaa 2011년 6월 3일
I will be thanful if you try to help me ....
this link descibe the problem i need to code it
http://en.wikipedia.org/wiki/Longest_common_subsequence_problem#LCS_function_defined

댓글을 달려면 로그인하십시오.

답변 (1개)

John D'Errico
John D'Errico 2011년 6월 3일
Well, you could always just use my commonsubstring function to do the work.
Of course, since this is almost surely homework, that is not really a viable option. commonsubstring is pretty fast though.

카테고리

Help CenterFile Exchange에서 Standard File Formats에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by