...

/

Dictionary, Tuple, Set, and Cell

Dictionary, Tuple, Set, and Cell

Learn about dictionaries, tuples, sets, and cell data types in MATLAB and Python.

Python has specific data types that are unavailable in MATLAB or any other programming language. These include dictionaries, tuples, and sets. We only have dictionaries in MATLAB 2022b version.

Unlike simple arrays in MATLAB, we can use cell array, which has similar features like list, tuple or set in Python.

Press + to interact
The list, tuple and set
The list, tuple and set

Dictionary

The dictionary data type was introduced in MATLAB 2022b. We need a key and value pair to initialize a dictionary. In MATLAB, both the key and value must be arrays.

Let’s implement a dictionary in MATLAB.

Press + to interact
ids = [1 2 3];
names = ["alice" "bob" "charlie"];
d = dictionary(ids,names) % initialize and print a dictionary
whos d % display data type of disctionary

The output of the above code is:

Press + to interact
dictionary (double ⟼ string) with 3 entries:
1 ⟼ "alice"
2 ⟼ "bob"
3 ⟼ "charlie"
Name Size Bytes Class Attributes
d 1x1 300 dictionary

In Python, a dictionary data structure stores the data in {key: value} pairs enclosed in braces. The key and value might not be of the same data type.

Press + to interact
d = {"First name": "alice", # initialization
"Age": 25,
"Hobbies": ["reading", "cooking"],
"is_avaliable": True }
print(d) # print dictionary
print(type(d)) # display data type of disctionary

The above example cannot be implemented in MATLAB because all the items of the key array and all the items of the value array in MATLAB ...