Why MATLAB uses the storage of variables in form of Metrics/multi dimentional arrays ?
2 次查看(过去 30 天)
显示 更早的评论
I mean when we store a variable a = 5; we can see in the variable editor that this variable is stored in the metric format as a kind of multi dimensianl array. Why MATLAB decided to use that format to store the variables in this format ? Using Array / stack / heaps will increase the work load and they are not very easy to compute..So how does this storage makes the functionality of MATLAB quicker ?
0 个评论
回答(3 个)
Guru
2013-7-5
What you see in the variable editor is based upon how developers thought it to be most intuitive for users to see the variable, not how it is actually internally stored. MathWorks after all has a rather sizeable Usability team that tries to find what customers expect as they use the IDE, including the Variable Editor.
Now, how MATLAB stores data is one giant vector. This is much the same way that C stores data, the only difference being is how the vector is arranged and interpreted. The storage is just a means, and not really intended to make MATLAB faster or slower.
0 个评论
Walter Roberson
2013-7-5
If MATLAB used a different storage variety to store scalars, then all code that handled variables or expressions will need to know about all the different storage formats. For example, if you construct a function, then because the same function might be called with a scalar or a vector for any given argument, MATLAB would need to internally track the different formats, because the function is not going to be able to "see" the variables in the routines the function is being called from in order to generate more specific code.
Yes, I know that C++ can generate more specific code in some cases, using "templates" or "overloaded functions", but the cost of that is that C++ needs to generate one version of the code for every variation actually used. And MATLAB is an interpreter, so which variations are going to be used is generally not going to be known until the statement is reached.
If MATLAB used a different internal storage format for 2 dimensions compared to 3 dimensions compared to 4, etc., then the same kinds of problems as above would occur. But that duplicate code could be eliminated if the internal storage format involved storing the number of dimensions and then the dimensions themselves. Which is what MATLAB already pretty much does.
The arrangement that MATLAB uses also makes it quite fast to "reshape" vectors and arrays -- just a matter of changing the header, leaving the storage alone. Reshaping happens quite a bit in science code, in practice.
I know of one project in which the authors put in more than 5 person-years writing the software in Java "because it is a clean language", only to find that the math algorithms came out much too slow because of all the work copying of array contents) that had to be done in Java to shift representations between dimensions. The authors gave up and rewrote the core in C++, which only had to "cast" the pointers instead of copying the contents. MATLAB's reshape() can effectively act like a pointer "cast" between representation dimensins.
0 个评论
Jan
2013-7-5
编辑:Jan
2013-7-5
Matlab was developped to work with matrices efficiently. Therefore the program is called "MAT"rix"LAB"oratory. So it is not the right questions, why matrices are preferred, because this was the intention of the software.
Of course a bunch of scalars can be stored more efficiently. And this concerns one of the most frequently asked questions also: Beginners want to store a set of values in the variables A1, A2, A3, ..., and the experienced Matlab users teach them to use an array instead: A(1), A(2), A(3), ...
Arrays with more than 2 dimensions and cell arrays have been introduced in later releases, as far as I remember 4.1 or 5.0.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Performance and Memory 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!