Hướng dẫn can python replace matlab

There are three aspects to consider when replacing Matlab with Python; the core language capabilities, the IDE and the Price.

Capabilities Comparison

This section outlines all of the capabilities of each platform. In short everything Matlab can do, Python can also do plus much more. However, things like linear algebra and rapid prototyping Matlab does more cleanly.

Matlab

Everything in Matlab is ready bundled and toolboxes are seamlessly integrated. It is also much more mature - but was not designed for general purpose programming meaning anything not linear algebra related is painful to implement.

  • Toolboxes: The Matlab toolboxes are very good and python implementations often struggle to compete with them [even for not so specialised things like optimisation algorithms].
  • Linear Algebra: Matlab has concise linear algebra capabilities which are easier to read and interpret.
  • Visualisation Capabilities: Has very good and very broad range of visualisation capabilities.

Python

Python will require a number of additional modules for it to become a good Matlabreplacement.

  • General Purpose Programming: Python is a proper programming language which offers a lot more flexibility over Matlab.
  • Numpy: Provides arrays and linear algebra routines, however has more awkward syntax than Matlab. I have fallen in love with the broadcasting feature however, no more repmat rubbish.
  • SciPy: Stats, optimization and other useful mathematical stuff.
  • MatPlotLib: Provides, in my opinion, higher quality plots than Matlab. Furthermore it is designed to be syntactically similar to Matlab so should be easy to pick up.
  • SymPy: Good symbolic programming package.

IDE Comparison

This is where python is lacking in my opinion. I simply find algorithm development [particularly involving a lot of linear algebra] less painful in the Matlab IDE.

Matlab

Here are some key Matlab features which will be hard to live without as an engineering student. Note that Python IDEs do have some of these capabilities also, but are more sloppily implemented.

  • Powerful Debugging: Can easily explore functions in a very non-restrictive manner. Python also allows for debugging but is often not as cleanly implemented in the IDEs.
  • Variable Explorer: MATLAB has a good variable explorer which always good for viewing of many supported data types. Some python IDEs struggle to display numpy arrays which can be annoying.
  • Variable Survival: If code breaks during runtime, the variables still persist which helps debugging. Furthermore it is easier to run different scripts in a row assuming persistent variables. In Python you will have to manually save variables in a script to have them available for other scripts.
  • Profiler: Very good for a breakdown of where your algorithms are bottlenecking.
  • Command Line Single line instructions can be executed on the command line. Python is full support for this also in all good IDEs.
  • GUI Plotting Tools: Results and variables can be quickly and painlessly plotted from the user interface. Most Python IDEs are geared towards software development and so don't support this [although sypder does have limited support through matplotlib].
  • Documentation: All functions have native documentation within the IDE.

Python

There are some new python IDEs which are starting to compete with some of MATLAB's key advantages. I personally like PyCharm which was recently released as a free community version. It has the following capabilities and is very well designed from a UI perspective.

  • Bebugging
  • Variable Explorer
  • Command Line
  • Documentation

Price Comparison

Python is free and has an active support community. Matlab also had a good support community too, but is anything but free.

Nội dung chính

  • Python for Matlab Users, Part 1: Python vs Matlab
  • Why Python?
  • 10 Reasons Python is better than Matlab
  • 10 Reasons Matlab is better than Python
  • Is it better to learn Python or MATLAB?
  • Can you use Python like MATLAB?
  • Which is faster Python or MATLAB?
  • Is MATLAB easy or difficult?

A Python might look like a daunting snake just waiting to bite you, but it can actually be a great be a great pet once you get to know it. Photo by Jan Kopřiva on Unsplash

Python for Matlab Users, Part 1: Python vs Matlab

Maybe you’re an engineer like me who’s been using Matlab for quite some time and have grown super attached to it. You are probably fluent in the Matlab language, and whenever you face a problem you immediately start thinking in matrices and indexing notation.

You know that Python is 0-index’ed and already now you start questioning if such a language is really the way to go

But you can’t avoid listening when you hear engineers and data scientists talk about Python. How it’s one of the fastest, easiest languages to learn, and how it can do what Matlab can do — and much more. You then look at your most recent invoice from Mathworks and start questioning if a free-to-use programming language might actually be the way to go.

But at the same time, you know that Python is 0-index’ed and already now you start questioning if such a language is really the way to go. Then you hear weird terms such as list comprehensions and you start to get slightly scared. You do a bit of searching around the internet and stumble over ugly, scary-looking code snippets such as the following:

X = dict[zip[VarNames, [[np.array[data[name],dtype=float].flatten[]] for name in VarNames] ] ]

And now you start to wonder why anyone would use such a language.

But you are stubborn. Just like me.

And you know that just because you don’t quite understand what all the fuzz is about, you also know that so many people can’t be wrong. So you Google “Python for Matlab users” and now you’re here.

So what is this? Well let me tell you:

In this blog series, I will explain how to transition from being a hardcore Matlab user, to becoming a Python fan.

A word of caution though:

This is not an expert’s guide to using Python.
This is a beginner’s guide — as in I’m the beginner.

What this means for you is that I am writing this as I go, but after clearing up all the mistakes and dead ends I’ve taken, so you don’t have to repeat them. This writeup is therefore based on a lot of Googling around and plain and simple trial and error. But enough chatting — let’s start digging a bit deeper.

Why Python?

The first step in using Python, is to actually decide to use it. There are many reasons why one might consider using Python, and I’ve listed the main ones for me in the following. Similarly, there are also good reasons why Matlab is superior to Python. These are also listed below.

An advantage of Python is the support for multiple IDEs meaning you can pick you own syntax highlighting. Photo by Chris Ried on Unsplash

10 Reasons Python is better than Matlab

  1. It’s free.
    This one is simple, but one of the most important ones for anyone working outside academia. As Matlab is an expensive software, many companies only have one [if any] license. For this reason, using a free alternative might be attractive for many companies.
  2. It’s open source
    Having a language where everyone can contribute to the development of it means new features will constantly be added and bugs will be fixed. It also means you can go and inspect exactly how the functions you use works. This also aids in the growth of the language and helps making a big community of advanced users and contributors.
  3. It’s the future
    The popularity of Python has just kept increasing in an exponential fashion over the past years. Having a more popular language means it’s easier to find answers to any questions you may have, and to find code examples of what you need.
  4. It has more features
    Unlike Matlab, Python is not just a scripting language for math — it’s also an imperative and function language which can be used for crawling web-servers, controlling external devices or making user interfaces.
  5. It’s portable
    Python is, just like Matlab, a cross-platform, language which can run on al OSs — even embedded systems having a small Linux kernel. Deploying Python code is also easier — you just need to install python [which comes by default in many OSs] and not deal with having the right version of Matlab runtime. This also means it is super easy to deploy Python code to servers.
  6. It’s THE go-to language for machine learning
    With the increasing popularity of machine learning and AI, Python is light years ahead of Matlab, as all major frameworks are based on Python: Tensorflow, Keras, PyTorch, Scikit-learn. And since all AI research is made using these frameworks it is way easier to find state of the art algorithms for Python than Matlab.
  7. It’s highly flexible
    In Python, there are many ways to achieve the same functionality. Some are of course more efficient than others, but having a language which allows you to do things the way that suits you is highly appreciated.
  8. It allows for using different IDEs
    When using Matlab you’re forced to use the Matlab IDE shown in the image below. Luckily the Matlab IDE works quite well, but you are also quite limited to the features that Matlab has chosen to implement: For instance is Git support quite poor. As Python can be compiled from the command-line many different IDEs are available — from simple text editors to full-fledged Matlab-like solutions.
  9. Simpler [prettier] language
    Even though it might not appear so at first sight, Python can actually produce much simpler, and thus prettier, code than Matlab. One such example is in for-loops where you can get both the index i and i’th item when iterating over an array.
  10. Named arguments
    Named arguments in Python lets you call a function ala. mean[X, axis=1], where you in Matlab would write mean[X,1] i.e. it would be unclear what the 1 is used for. This makes the code much easier to read and debug.

The Matlab IDE. Photo from Wikipedia

10 Reasons Matlab is better than Python

  1. It “just works”
    Since Matlab is closed-source they also guarantee that when you install it, all the necessary bits [no pun intended] and pieces are there for Matlab to run successfully. This is not guaranteed in Python. Fortunately, I am here to help and have made a guide on how to install Python.
  2. There’s a big company behind it
    Since Matlab is made by Mathworks, they also guarantee the quality and correctness of their product [that’s what you’re paying for if you are in doubt!]. It also means there’s a support team to write to should you have any problems with their product.
  3. Simulink
    Depending on your field of engineering, you may or may not use Simulink. For some, it is an absolutely must-have, whereas others [myself included] have barely opened it.
  4. Toolboxes
    This again depends on your field of engineering, but communications engineers, having the LTE toolbox makes life much easier, while the optimal control toolbox and system identification toolboxes are hard to live without for most control engineers.
  5. It executes faster
    This point might open up for a great deal of debate, but my impression is that Matlab code by default executes faster than Python, simply because many Matlab functions are pre-compiled by default, while the user has to do this manually in Python.
  6. Better debugging
    The Matlab debugger is amazing: The ability to click to insert a breakpoint in a sub-function and then inspect all the functions’ variables [including variables of higher-level functions, by changing the call stack] is a super powerful tool . Python also has a debugger, but it’s way more complicated to get to work, and is by no means as simple as the Matlab debugger and it honestly just doesn’t work as well.
  7. No packages to include/manage
    Since Matlab installs as a full program, all the necessary libraries are included in the installation. This means once they are installed there is no need to remember to include the various toolboxes to use their functionalities — they just work.
  8. You already know how to use it
    This is probably the strongest argument for Matlab, and the hardest one to combat. There is just no way of beating +10 years of experience using a certain language — no matter how clever the alternatives might be.
  9. Your entire code-base is using it
    Just as there is no way of beating +10 years of coding experience, there is also no way of beating +10 years of scripts, functions and those driver functions you wrote which you keep using on a day-to-day basis. And let’s be honest: You don’t have the time, nor desire to port them to Python.
  10. Your peers are using it
    And finally, another argument which is hard to do anything about: If he entire scientific community in your field [or company] are using Matlab, then you’ll undoubtedly be sharing Matlab code back and forth. Unless you become that guy who uses Python. But you want to be in the inner circle with all the other Matlab nerds. Ahh, if only everyone else was using it, it would be much simpler to make the switch. But someone has to be the front-runners in this world! Why not you?

If only everyone else was using it, it would be much simper to make the switch

I can’t tell you which of the advantages outweigh the others — that depends entirely on the context and the situation of each individual Matlab user. To me, many of the reasons to stick with Matlab are related to the momentum gained by having used Matlab for years and feeling “at home” with the software. And being afraid of taking the leap and putting in the work.

But, if this read hasn’t entirely thrown you off and you are motivated to give Python a try, let’s continue to the next part where we actually install Python!

Is it better to learn Python or MATLAB?

In summary, it is good to have both but definitely start off with python. I personally prefer MATLAB. When you are working on scientific computing, particularly if you need to handle matrices and vectors then MATLAB will give you the best experience. But it is quite costly, so many students prefer Python.

Can you use Python like MATLAB?

For all of these reasons, and many more, Python is an excellent choice to replace MATLAB as your programming language of choice. Now that you're convinced to try out Python, read on to find out how to get it on your computer and how to switch from MATLAB! Note: GNU Octave is a free and open-source clone of MATLAB.

Which is faster Python or MATLAB?

For this example, Matlab is roughly three times faster than python.

Is MATLAB easy or difficult?

It uses the programming system and language called MATLAB to do so because it is easy to learn, versatile and very useful for engineers and other professionals. MATLAB is a special-purpose language that is an excellent choice for writing moderate-size programs that solve problems involving the manipulation of numbers.

Chủ Đề