replace *.m file program code with matrix manipulation!

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 Comments

I guess the grammatical error has been removed.
John, I am waiting for you to comment on the substance of the question.
I'm not sure what your question is. Do you have an actual m file you want to do this for, or are you asking for a tool that would do this? Also, do you mean it should replace * with .* or should it be doing the reverse? Or do you want an effect similar to arrayfun?
Hi Rahul,
so would this include, for example, replacing
x = [1 3 6 5 4]
y = sort(x)
% or
x = [1 3 6 5 4]
y = max(x)
% or
x = 1.7
y = round(x)
% or
if-then-else structures
% or ...
My intention was to check community response to my idea.
I don't have a tool or automation in mind as yet. Just the possibility is a concern.
If we could take a typical *.m program and convert it, statement by statement, to equivalent matrix manipulation then we stand a chance.
I am working on the list of program statements, Data declaration Control flow Function calls ..in order to convert them from typical forms to equivalent matrix manipulation instructions
Data declarations and algebraic constructs are no issue, For loops are OK, While loops are a problem yet.
I'm very unclear on what you call a typical .m program and what you call the equivalent matrix manipulation. Can you show an example?
What I mean is a 'script' which has the following programming constructs.. 1...data declaration 2...control flow statements 3...function calls 4....is a function definition, maybe. 5...The matrix operations.
By matrix manipulation instructions, I mean all function calls inbuilt into MATLAB called matrix operations.
"Can we can replace all programming language statements/instructions in an *.m file with matrix manipulation commands?"
There is no trivial equivalent for recursion, de/serialising operations, asynchronous code, etc.
"We might get efficient code."
MATLAB's inbuilt routines are usually highly optimized. While in theory everything that your computer does could be reduced to operations on lists/vectors (and deep in the compiled machine code probably is), in general you are unlikely to be able to improve on the speeds offered by MATLAB's optimized specialist routines.
"Maybe, readable and maintainable code, also!"
Why do you think that?
As of now I think that it is not possible to replace the while loop. I find it tragic that we are not allowed a goto statement in MATLAB.
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.
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.

Sign in to comment.

Answers (0)

Categories

Find more on Scripts in Help Center and File Exchange

Products

Asked:

on 28 Sep 2019

Edited:

on 4 Oct 2019

Community Treasure Hunt

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

Start Hunting!