- array has any size RxCxPx....(x1x1x1x1...), it can be numeric/logical/char class
- matrix is an array with size RxC(x1x1x1x1...)
- vector is a array with size 1xC(x1x1...) or Rx1(x1x1x1...).
- scalar is a array with size 1x1(x1x1x1...)
scalar vs matrix vs vector vs array??
94 views (last 30 days)
Show older comments
While using MATLAB and using different internet sources and even MATLAB documentation,we often see the terms scalar, matrix ,vector and array. But what is their meaning and usage in context of MATLAB and what is difference between all of them??. Please kindly explain in simple words with example
Also please guide,wether they all four :scalar,matrix,vector and arrays belong to default class/datatype "double"?
0 Comments
Accepted Answer
Stephen23
on 13 Feb 2020
Edited: Stephen23
on 13 Feb 2020
Just like in mathematics, the only difference is their size.
MATLAB does not have special classes for scalars, vectors, matrices, or arrays: they are all arrays:
The usual definitions are:
That is all.
4 Comments
Rik
on 16 Mar 2021
They can, but don't need to. double is the default for numeric data in Matlab. Stephens answer applies to most data types. The first exception I can think of is a table, but there are probably more.
More Answers (2)
Walter Roberson
on 13 Feb 2020
- scalar: has exactly one value associated with it. Not empty, not more than one value.
- vector: an array that is (1 x something) or (something x 1) with no other dimensions. This includes arrays that are 0 x 1 or 1 x 0, so vectors can be empty. Also includes arrays that are 1 x 1, so vectors can be scalar. However, the standard symbol [] happens to represent a 0 x 0 array, so isvector([]) is false.
- matrix: an array that is m x n with no further dimensions. This includes arrays that are 0 x 0, 0 x something, something x 0, 1 x something, something x 1, so matrix includes scalars and vectors and [] .
- array: any arrangement of objects of any dimension. This includes all scalars and vectors and matrix.
This leads us to:
- [] is not a scalar and not a vector, but is a matrix and an array
- something that is 0 x something or something by 0 is empty. It is never a scalar, but could be a vector if it is 0 x 1 or 1 x 0. It is also a matrix and also an array
- all scalars are also vectors, and all scalars are also matrix, and all scalars are also array
- all vectors are also matrix, and all vectors are also array. vectors are only also scalars if they happen to be 1 x 1
- all matrix are also array. matrix are only also scalars if they happen to be 1 x 1. matrix are only also vectors if they happen to be 1 x something or something x 1
- array are only also scalars if they happen to be 1 x 1 . arrays are only also vectors if they happen to be 1 x something or something x 1 . arrays are only also matrix if they happen to be m x n with no more dimensions.
- arrays that have 0 for any dimension are empty, even if the 0 isn't until the 267'th dimension. Empty arrays can be any size for the other dimensions: an array that is 166667 x 3423435 x 0 x 34246 is empty . Empty arrays are never scalars. Empty arrays are only also vectors if they are 0 x 1 or 1 x 0. Empty arrays are only also matrix if they are 0 x n or m x 0.
0 Comments
Steven Lord
on 13 Feb 2020
Please see the Description sections on the documentation pages for the isscalar, isvector, and ismatrix functions for a definition of what MATLAB considers a scalar, vector, and matrix to be. You can find all three of those functions linked from this documentation page.
Everything in MATLAB can be considered an array, but usually when I use the word array instead of matrix I'm using it to denote either something with an unknown or unspecified number of dimensions (possibly greater than 2) or to denote something that I know has more than 2 dimensions.
"The sin function accepts an array x and returns an array y of the same size. Each element in y is the sine of the corresponding element of x." Here, I don't know how many dimensions x has so I use the most general term. The statement is true whether I call sin on a scalar, a vector, a matrix, or a 1-by-2-by-3-by-4-by-5-by-6 array.
"A truecolor array has three pages: one for the red component, one for green, and one for blue." Here I know that the thing to which I'm referring, the truecolor image, has three dimensions so it's not a matrix.
"Calling the magic function as magic(n) returns the n-by-n magic square matrix." By definition magic returns a 2-dimensional array so using the word matrix is accurate and most specific.
And yes, I know magic(2) doesn't return a magic square.
0 Comments
See Also
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!