replace *.m file program code with matrix manipulation!

1 vue (au cours des 30 derniers jours)
RahulTandon
RahulTandon le 28 Sep 2019
Can we can replace all programming language statements/instructions in an *.m file with matrix manipulation commands?
We might get efficient code. Maybe, readable and maintainable code, also!
The effort is worth the while. imagine efficient code made more so by Mex.
  11 commentaires
Guillaume
Guillaume le 4 Oct 2019
I find it tragic that we are not allowed a goto statement in MATLAB.
Decades of experience have shown that goto statements are better left out in high-level languages and instead branching statements are much more useful.
Of course, at the machine level, branching statements end up being jump operations.
It's still not clear to me what you call matrix operations. Note that what you may think of a simple matrix operation in matlab (e.g. +) will involve internally loops, branches, etc.
John D'Errico
John D'Errico le 4 Oct 2019
Modifié(e) : John D'Errico le 4 Oct 2019
I find it tragic that you seem to desperately need a goto statement. There are many constructs in MATLAB that overcome the need for one. For example, while loops, functions, continue, break, if-else statements, etc. The list is a lengthy one. So the answer is to just learn MATLAB. Just understanding how to use functions effectively and some of those simple program flow control constructs will allow you to elminate all of that need. Sorry, but in 30 years of heavy MATLAB use, I've never wanted a goto.
As far as replacing all m-file code with matrix manipulations, again, this just means you need to learn MATLAB. Some operations are trivially replaced. Others are not. Until you learn sufficient coding skills in MATLAB to know when to use those operations, you are stuck, wishing things would just work magically, but needing to write looped code.
You need to learn how to use matrices effectively. There is no replacement for experience here. That means fiully understanding how elements are stored in memory in a matrix, so you can manipulate them well. Tools like permute, reshape, transpose, are all valuable. IF you are living in an old MATLAB release, then bsxfun will be invaluable. Newer releases can benefit from implicit scalar expansions.
Next, you need to understand how to work with all the common array forms, so double arrays in multiple dimensions, cell arrays, structs, etc. Then learn to use tools like arrayfun and cellfun. Other tools, like discretize, regular expressions, yada, yada, yada...
All of these things will allow you to improve your coding in MATLAB, then to know when you can indeed replace some lengthy written code, with sleeker constructs.
But in the end, sleekly written highly vectorized code is not always the best code. The best code is code that executes in a reasonable amopunt of time. It is code that is readable, and maintainable. Highly vectorized code is often not easily read, so it needs to be carefully documented since you will need to debug it next year when you have completely forgotten what it does and how it works.
And remember that highly vectorized code that uses matrices (and whatever your favorute construct may be) need not always be efficient code! I can give you many examples of code that looks wonderful. Is shortly written, but takes forever to run. That is often because highly vectorized code is often a memory hog, replacing external loops with internal loops, where all of your data is processed at once. But then you need to store large intermediate arrays.
As for the replacement of m-code by mex, that is often a mirage. As long as the underlying built-in code is efficient, then compiled m-file code does not gain much at all, since the parser is already pretty efficient. And automatically compiled code is not efficiently written c-code, so you don't gain much there. The trick is to use vectors, arrays, etc., efficiently, and then you will have no need at all for compiled code to give a speed boost. Again, the idea is to learn to use MATLAB effectively.

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Debugging and Analysis dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by