Grok all the things

grok (v): to understand (something) intuitively.

MATLAB

🙇‍♀️  Students & Apprentices

Welcome, fellow MATLAB enthusiasts! Today, we're diving into one of the most powerful and versatile programming languages out there: MATLAB. If you're passionate about computation, data analysis, and modeling, then you're in for a treat. Fasten your seatbelts as we explore the intriguing world of MATLAB, its history, and those eccentricities that make it so unique.

🌟 A Brief History

The story of MATLAB is nothing short of fascinating. It all began in the late 1970s, when Cleve Moler, a professor at the University of New Mexico, created a simple tool to help his students with linear algebra and matrix computations. Little did he know that his humble creation would soon become a global phenomenon.

MATLAB, which stands for MATrix LABoratory, was formally launched in 1984 by MathWorks, founded by Moler and Jack Little. Today, MATLAB is used in countless industries, from engineering and finance to neuroscience and robotics!

Now that you have a bit of context, let's jump into the heart of MATLAB!

💡 Language Basics

MATLAB is an interpreted language, which means it executes code line by line instead of compiling it first (like C++ or Java). It's designed primarily for numerical computing and uses matrices as its core data structure.

Variables 🏷️

You don't need to declare variables explicitly in MATLAB. Just assign a variable a value, and MATLAB takes care of the rest.

a = 42;
b = "Hello, MATLAB!";

Matrices 🏢

Creating matrices (the bread and butter of MATLAB) is painless:

A = [1, 2, 3; 4, 5, 6; 7, 8, 9]; % A 3x3 matrix

Notice that semicolon ;? In MATLAB, it suppresses output. Without it, MATLAB would print the result to the console.

Indexing 🔍

MATLAB uses 1-based indexing instead of 0-based indexing, like Python or C++. Let's access our matrix A:

first_element = A(1, 1); % Top-left element
last_element = A(3, 3); % Bottom-right element

Semicolons vs. Commas

When creating a matrix, you may have noticed that we used commas , and semicolons ;. Commas separate elements within rows, while semicolons separate rows themselves:

B = [1, 2, 3; 4, 5, 6]; % A 2x3 matrix

📚 Functions & Script Files

MATLAB scripts are simply sequences of commands stored in a file with the extension .m. Functions, on the other hand, are more sophisticated and allow you to define input and output parameters.

Here's a simple script that adds two numbers:

% add_numbers.m
a = 2;
b = 3;
result = a + b;
disp(result);

Now let's convert this to a function:

% add_numbers_func.m
function result = add_numbers_func(a, b)
    result = a + b;
end

To call this function:

sum = add_numbers_func(2, 3);
disp(sum);

📈 Plotting

MATLAB is well-known for its remarkable plotting capabilities. Let's take them for a spin. Here's how you can plot a simple sine wave:

x = linspace(0, 2 * pi, 100); % Generate 100 evenly spaced points between 0 and 2*pi
y = sin(x);
plot(x, y);
xlabel('x-axis');
ylabel('y-axis');
title('Sine Wave');

That's it! You've just plotted a beautiful sine wave.

𝚺 Weird & Wonderful MATLAB Facts

To truly grok the essence of MATLAB, we must dive into its peculiarities.

Matrix Inception 🔄

In MATLAB, everything is a matrix! Even scalar values and strings are treated as matrices. Take a look:

position = [1, 2];
greeting = "Hello, world!";

The variable position is a 1x2 matrix, while greeting is a 1x13 matrix composed of characters!

Heterogeneous Arrays 🔀

MATLAB supports creating heterogeneous arrays (cell arrays) that can store different data types within a single array:

weird_array = {42, "Answer", [1, 2, 3]};

To get values from a cell array, use curly braces {}:

value = weird_array{1}; % Sets value to 42

Double-Colon Operator ⏭️

The double-colon operator :: lets you create custom sequences or select elements from a matrix:

even_numbers = 0:2:10; % Creates an array [0, 2, 4, 6, 8, 10]
third_row = A(3, :); % Selects the third row of matrix A

🌐 Community Resources

As you embark on your MATLAB journey, you'll find it invaluable to tap into the supportive and knowledgeable MATLAB community. Here are some resources to help you along:

  1. MATLAB Central - The official MATLAB community portal, featuring forums, tutorials, and contests.
  2. r/matlab - A bustling subreddit filled with passionate MATLAB enthusiasts.
  3. MATLAB Discord - A friendly Discord server where you can chat and collaborate with fellow MATLAB users.

That wraps up our whirlwind tour of the wondrous world of MATLAB. We've seen how it emerged from humble beginnings to become a global powerhouse in computation, learned about its intriguing matrix-based data structures, and dabbled in its eccentricities.

There's just so much more to explore, but hopefully, you now have a deeper appreciation for MATLAB and feel excited to dig in further!

Grok.foo is a collection of articles on a variety of technology and programming articles assembled by James Padolsey. Enjoy! And please share! And if you feel like you can donate here so I can create more free content for you.