Can you use python like matlab?

What Is Python?

Python is a general-purpose programming language with an extensive collection of libraries that are used for a wide range of applications, including web development, enterprise application development, and data science. 

What Is MATLAB?

MATLAB is a computing platform that is used for engineering and scientific applications like data analysis, signal and image processing, control systems, wireless communications, and robotics. MATLAB includes a programming language, interactive apps, highly specialized libraries for engineering applications, and tools for automatically generating embedded code. MATLAB is also the foundation for Simulink, a block diagram environment for simulating complex multi-domain systems.

Language Comparison

The language of Python and MATLAB can be used interactively [a single command at a time] or to develop large-scale applications. Both languages support scripting, procedural and object-oriented programming.

Python has largely replaced Java as the first language for people who want to learn how to program because it is easy to learn and can be used for various programming tasks. MATLAB language is the first [and often only] programming language for many engineers and scientists because the matrix math and array orientation of the language makes it easy to learn and apply to engineering and scientific problem-solving. Apps and other interactive tools automatically generate MATLAB code, further reducing the barrier to entry.

User Base

Both Python and MATLAB have large user bases, though the user base for MATLAB is primarily comprised of engineers and scientists. As of May 2022, LinkedIn searches return about 7.6 million Python users and 4.1 million MATLAB users. People who do not work in engineering or science are often surprised to learn how widespread MATLAB is adopted, including:

  • Millions of users in colleges and universities
  • Thousands of startups
  • Thousands of people at every major company and organization where engineers and scientists work, such as Apple, Google, Airbus, Tesla, and the International Monetary Fund

Support and Documentation

Stack Overflow and similar sites are the primary support mechanism for most Python users. With so many users worldwide, it is easy to find or start a discussion online that can help answer just about any question. Documentation is available via docstrings and individual websites for Python and most libraries.

MATLAB licenses include free live support, so users can pick up the phone or send an email to get help from a MATLAB expert on their specific project. Additional free support from MathWorks worldwide engineers can help users figure out the best way to assemble available technology for their application, research, or teaching. MATLAB Answers provides an online question and answer forum specific to MATLAB, similar to Stack Overflow. MATLAB includes an integrated documentation system that combines professionally authored documentation for all MATLAB and Simulink products, help for user-authored packages, and additional online resources such as MATLAB Answers and videos.

Cost

Python and most Python libraries are free to download or use, though many users use Python through a paid service. Paid services help IT organizations manage the risks associated with the use of open-source software, such as security, licensing, and access control. Paid services also provide access to packaged or hosted distributions that eliminate much of the pain of configuring a Python installation with compatible versions of libraries.

MATLAB is not free, though it is more accessible and costs less than people assume. Approximately 8 million people have unlimited access to MATLAB through their school, research institution, or employer, including at most universities worldwide that grant engineering and science degrees. Institutions choose to invest in MATLAB because they recognize the value of the increased productivity from providing their engineers and scientists with purpose-built tools.

MATLAB and Python

While most people start by asking whether they should choose MATLAB or Python, the right answer is often to use MATLAB and Python together. With MATLAB and Python, you can take advantage of the best capabilities of each environment. For instance, you can:

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!

Chủ Đề