I've just started with MATLAB, having mainly played around with Python previously. I've just made my first associative array, and am a little confused with how it's dealing with commas and spaces. My array is:
co_comma=containers.Map({'Open University','UCL',' University of Edinburgh','Birkbeck'},{193835,21210,24525,17822})
I also made a second associative array, splitting using spaces:
co_space=containers.Map({'Open University' 'UCL' ' University of Edinburgh' 'Birkbeck'},{193835 21210 24525 17822})
They both give the following:
Map with properties:
Count: 4
KeyType: char
ValueType: double
But co_comma==co_space
gives False
:
ans =
logical
0
Questions:
Thanks :D
Many things mixed in here!
Clarifications:
A matlab container is only equivalent to a python dictionary, not a list/tuple/general iterable.
Both of your containers are created the same. You seem to be naming them comma and space, but this distinction does not even reach the definition of the container.
Both {'Open University','UCL',' University of Edinburgh','Birkbeck'}
and {'Open University' 'UCL' ' University of Edinburgh' 'Birkbeck'}
create the exact same cell array, so the input to container.map
is the same. you are comparing a=[5];
and b=5
, as a MATLAB-valid analogy. They are the same.
For objects, most programing languages (including python!) will give you false when you compare two objects that contain the same values, yet are different objects. Only basic variables tend to compare by value, and not by some sort of objecID
. In your case, simply doing isequal(co_comma,co_space)
will return true
, so their values are the same (as we already know, from the previous point)
Containers are not generally used in MATLAB, unless you specifically want a dictionary.
So here's the deal with Matlab: Matlab is an oddball. In Matlab, everything is an array, and there is no real distinction between regular "values" and "containers" or "collections" like there is in many programming languages. In Matlab, any numeric value is a numeric array. Any string
value is actually an array of strings. Every value is iterable and can be used in a for
loop or other "list"-like context. And every type or class in Matlab must implement collection/container behaviors as well as its "plain" value semantics.
Scalar values in Matlab are actually degenerate cases of two-dimensional arrays that are 1-by-1 in size. The number 42
? Actually a 1-long array of doubles. The string "foo"
? Actually a 1-long array of strings. Everything in Matlab is actually like a list in Python (or, more accurately, a NumPy series or array).
A cell
array is an array of things, which can contain any other type of array in each of its elements. This is used for heterogeneous collections.
A char
array is a bit weird, because it is used to represent strings, but its elements are not themselves strings, but rather characters. Plain char
arrays are used in a weird way to represent lists of strings: you make a 2-D char
array, and read each row as a string that is padded with spaces on the right. Lists of strings that are different lengths used to be represented as "cellstrs", which are cell
arrays that contain char
row vectors in each element. It's weird; see my blog post about it. The new string
array makes most uses of other string types obsolete. You might want to use string
s here. In Matlab, string
literals are double-quoted, and char
literals are single-quoted.
The word "container" doesn't have a specific technical meaning in Matlab, and is not really a thing. There's the containers
package, but the only thing in it is containers.Map
; there are no other types of containers and no generalization of what it means to be a container. One of the Matlab developers had an idea for making containers like this, but it never really went anywhere. And as far as I can tell, containers.Map
hardly gets used at all: containers.Map
is a pass-by-reference "handle" object, whereas most Matlab types are pass-by-value, so Map
can't be used easily with most Matlab code.
So, putting aside the weirdness of char
s, everything in Matlab has array semantics, and is effectively an iterable container like Python lists or tuples. And in Matlab, most values and objects are immutable and pass-by-value, so they are more like Python tuples than Python lists.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With