Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

can you help me with this matlab function?Given a string s = \'01111001000000010

ID: 3693287 • Letter: C

Question

can you help me with this matlab function?Given a string s = '011110010000000100010111', the length of the longest substring of s which contains consecutive ‘1's would be 4. Write a function named longest_one , which accepts one input string consisting only of ‘0’ and ‘1’ characters. The function should return the size of the longest substring of consecutive ‘1’s. You are required to use the programming method (loops, conditional statements). The function should return the desired output regardless of the input string size . Here's an example of a function call: longest_one('011110010000000100010111') ans = 4

Explanation / Answer

function [D, dist, aLongestString] = LCS(X,Y) %%%Calculates the longest common substring between to strings. %%%Code written by David Cumin %%%email: d.cumin@auckland.ac.nz %%%INPUT %%%X, Y - both are strings e.g. 'test' or 'stingtocompare' %%%OUTPUT %%%D is the substring over the length of the shortest string %%%dist is the length of the substring %%%aLongestString is a sting of length dist (only one of potentially many) %%%For example %%% X = 'abcabc'; %%% Y = 'adcbac'; %%% [D dist str] = LCS(X,Y); %%% results in: %%% D = 0.6667 %%% dist = 4 %%% str = acbc %%% this is seen for X: 'a-c-bc' and Y: 'a-cb-c' %%%Make matrix n =length(X); m =length(Y); L=zeros(n+1,m+1); L(1,:)=0; L(:,1)=0; b = zeros(n+1,m+1); b(:,1)=1;%%%Up b(1,:)=2;%%%Left for i = 2:n+1 for j = 2:m+1 if (X(i-1) == Y(j-1)) L(i,j) = L(i-1,j-1) + 1; b(i,j) = 3;%%%Up and left else L(i,j) = L(i-1,j-1); end if(L(i-1,j) >= L(i,j)) L(i,j) = L(i-1,j); b(i,j) = 1;%Up end if(L(i,j-1) >= L(i,j)) L(i,j) = L(i,j-1); b(i,j) = 2;%Left end end end L(:,1) = []; L(1,:) = []; b(:,1) = []; b(1,:) = []; dist = L(n,m); D = (dist / min(m,n)); if(dist == 0) aLongestString = ''; else %%%now backtrack to find the longest subsequence i = n; j = m; p = dist; aLongestString = {}; while(i>0 && j>0) if(b(i,j) == 3) aLongestString{p} = X(i); p = p-1; i = i-1; j = j-1; elseif(b(i,j) == 1) i = i-1; elseif(b(i,j) == 2) j = j-1; end end if ischar(aLongestString{1}) aLongestString = char(aLongestString)'; else aLongestString = cell2mat(aLongestString); end end end

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote