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

Matlab- Write a function [output_string] = deleteDups(string in) that takes an i

ID: 3810565 • Letter: M

Question

Matlab- Write a function [output_string] = deleteDups(string in) that takes an input string of some length and returns an output string without any duplicate characters, and in the order in which the characters appeared in the string. This can be done with a single for loop to index through the string and keeping only those characters not yet encountered. Now write a function A = SortLength(X) that takes an input cell array X containing strings of varying lengths, then applies the deleteDups function to each string, and outputs a cell array A containing the duplicate deleted strings sorted in order of string length from largest to smallest.

Explanation / Answer

function [output_string] = deleteDups(in)
  
[r,c] = size(in);
output_string = "";
for i = 1:c
a = ismember(in(i),output_string);
if(~a)
output_string = strcat(output_string,in(i));
end
end
  
end

function A = SortLength(X)
  
len = numel(X);
i = 1 ;
for str = X
X(i) = deleteDups(str{1});
i++;
end
A = {};
for str = X
a = ismember(str{1},A);
if(~a)
A(end+1)=str{1};
end
i++;
end
end


A = SortLength({"abcbac","eeefee","abc"})