Theodore asked . 2020-11-18

Matlab: optimize code

Problem: I have the trajectories of 14 users: I have compare the trajectories of every user and for each one, I have calculated the longest common sequence: TrajSimilarity(1,k).aLongestString. Now I want to compare the longest common sequence of the first user with the others, the longest common sequence of the second user with the others and etc.
 
I have no problems to compare the longest common sequence of two users using this code:
 
string3=TrajSimilarity(1,3).aLongestString;
string4=TrajSimilarity(1,4).aLongestString;
[D,dist,aLongestString]=LCS(string3,string4);

U=numel(regexp(string3,'(\(\d+\)|\d)'));
V=numel(regexp(string4,'(\(\d+\)|\d)'));
nLCS=numel(regexp(aLongestString,'(\(\d+\)|\d)'));

ratioU=nLCS/U;
ratioV=nLCS/V;

EA = (ratioU + ratioV)/2;
WA = (U*ratioU+V*ratioV)/(U+V);

LCS is a function that calculates the longest common substring between two strings (if can be helpful, I will post a link with it). I have problems to create an optimized code that compare the longest common sequence of the first user with the others, the longest common sequence of the second user with the others and etc. I try this code:

 

for k=1:14
   string3=TrajSimilarity(1,k).aLongestString;
    for j=2:14
        string4=TrajSimilarity(1,j).aLongestString;
        [A(k,j).D,A(k,j).dist,A(k,j).aLongestString]=LCS(string3,string4);
    end
end

but it generates error:

 

Attempted to access L(0,0); index must be a positive integer or
logical.

Error in LCS (line 52)
dist = L(n,m);

Error in Prova1 (line 6)
        [A(1,k).D,A(1,k).dist,A(1,k).aLongestString]=LCS(string3,string4);

Can you give me an help? 

matlab , control system ,

Expert Answer

Neeta Dsouza answered . 2024-04-28 11:39:29

You definitely need to post your LCS function for more detailed advice because that is the one that contains the error.
Somewhere in there you are attempting to index into an array using a 0-index which is not allowed as  Matlab arrays index from 1.
To fix it you need to either change the behaviour of LCS or change your inputs to that function ( string3, string4 ) in such a way that they fit to the expectations of the function in a way that m and n are > 1. That is assuming that m and n are dependent on 'string3' and 'string4' that are passed in (or ar in fact the same variables as those)
i.e. either
  • The function remains as is, but a prerequisite of its usage is that inputs must be > 1
or
  • The inputs stay as they are and the function must change its implementation and thus its expectations so that 0, 0 are valid inputs which give expected outputs.


Not satisfied with the answer ?? ASK NOW

Frequently Asked Questions

MATLAB offers tools for real-time AI applications, including Simulink for modeling and simulation. It can be used for developing algorithms and control systems for autonomous vehicles, robots, and other real-time AI systems.

MATLAB Online™ provides access to MATLAB® from your web browser. With MATLAB Online, your files are stored on MATLAB Drive™ and are available wherever you go. MATLAB Drive Connector synchronizes your files between your computers and MATLAB Online, providing offline access and eliminating the need to manually upload or download files. You can also run your files from the convenience of your smartphone or tablet by connecting to MathWorks® Cloud through the MATLAB Mobile™ app.

Yes, MATLAB provides tools and frameworks for deep learning, including the Deep Learning Toolbox. You can use MATLAB for tasks like building and training neural networks, image classification, and natural language processing.

MATLAB and Python are both popular choices for AI development. MATLAB is known for its ease of use in mathematical computations and its extensive toolbox for AI and machine learning. Python, on the other hand, has a vast ecosystem of libraries like TensorFlow and PyTorch. The choice depends on your preferences and project requirements.

You can find support, discussion forums, and a community of MATLAB users on the MATLAB website, Matlansolutions forums, and other AI-related online communities. Remember that MATLAB's capabilities in AI and machine learning continue to evolve, so staying updated with the latest features and resources is essential for effective AI development using MATLAB.

Without any hesitation the answer to this question is NO. The service we offer is 100% legal, legitimate and won't make you a cheater. Read and discover exactly what an essay writing service is and how when used correctly, is a valuable teaching aid and no more akin to cheating than a tutor's 'model essay' or the many published essay guides available from your local book shop. You should use the work as a reference and should not hand over the exact copy of it.

Matlabsolutions.com provides guaranteed satisfaction with a commitment to complete the work within time. Combined with our meticulous work ethics and extensive domain experience, We are the ideal partner for all your homework/assignment needs. We pledge to provide 24*7 support to dissolve all your academic doubts. We are composed of 300+ esteemed Matlab and other experts who have been empanelled after extensive research and quality check.

Matlabsolutions.com provides undivided attention to each Matlab assignment order with a methodical approach to solution. Our network span is not restricted to US, UK and Australia rather extends to countries like Singapore, Canada and UAE. Our Matlab assignment help services include Image Processing Assignments, Electrical Engineering Assignments, Matlab homework help, Matlab Research Paper help, Matlab Simulink help. Get your work done at the best price in industry.