Not able to understand matlab script
2 views (last 30 days)
Show older comments
Anshuman S on 25 Jul 2021
Answered: Shadaab Siddiqie on 28 Jul 2021
Can someone please help me in understanding this matlab sript in terms of mathematical equation.
+floor(nmolBC(j))/nmol(j)*(beta1R(j)*na1t + beta2R(j)*nb1t)*(N12(1,j)*pa12(1,j)-nR(j)*paR(j))))...
Steven Lord on 26 Jul 2021
I agree with Image Analyst's suggestion, though of course with more descriptive variable names than term1, term2, etc. Since people are going to need to try to read and understand this code in the future (potentially including you six months or maybe six days from now) if your code is at all complicated IMO you should err on the side of being descriptive. For example, that first term could be:
interactN1WithR = (m(1)./m(j))*b(j)*N(1)*(N(j)-nsupR(j));
That's still not a great name, but you know what your problem represents better than I do so you could choose an even better name. Though your LaTeX formatted equations seem a bit odd at first glance.
- Having In some places you refer to m as though it were a vector and in some places as though it were a matrix. That's fine in MATLAB, but if you're referring to just the first row or column of m when you use one index it might be clearer to make that explicit.
- Your use of both N and n could also be problematic; your finger slipping off the Shift key could change the results of your equations entirely.
Shadaab Siddiqie on 28 Jul 2021
From my understanding you want to improve the readability of the code. I agree with @Steven Lord to have more descriptive variable names. You can also leverage MATLAB editor tools to improve code readability. You could use the Apply smart indenting while typing option in the Preferences > MATLAB > Editor/Debugger > Language > Indenting section. You can refer Improve code readability for more information.
As for the existing code I would advise you to refactor the code with above mentioned improvements to make your code more readable.
Find more on Matrices and Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!