I have a loop in MATLAB that fills a cell array in my workspace (2011b, Windows 7, 64 bit) with the following entries:
my_array = [1x219 uint16] [ 138] [1x0 uint16] <---- row #3 [1x2 uint16] [1x0 uint16] [] <---- row #6 [ 210] [1x7 uint16] [1x0 uint16] [1x4 uint16] [1x0 uint16] [ 280] [] [] [ 293] [ 295] [1x2 uint16] [ 298] [1x0 uint16] [1x8 uint16] [1x5 uint16]
Note that some entries hold []
, as in row #6
, while others hold [1x0]
items, as in row #3
.
Answer: We usually reserve the word "vector" to denote an array that consists of only one column , i.e. is m-by-1, or only one row, i.e is 1-by-n. An array in MATLAB is a generic word that can mean a vector, a matrix, or a higher dimensional object, such as a "matrix" with three or more indices.
A scalar signal contains a single element. The signal could be a one-dimensional array with one element, or a matrix of size 1-by-1. A vector signal contains one or more elements, arranged in a series.
Array Creation To create an array with four elements in a single row, separate the elements with either a comma ( , ) or a space. This type of array is a row vector.
You also can use the {} operator to create an empty 0-by-0 cell array.
In most cases (see below for an exception) there is no real difference. Both are considered "empty", since at least one dimension has a size of 0. However, I wouldn't call this a bug, since as a programmer you may want to see this information in some cases.
Say, for example, you have a 2-D matrix and you want to index some rows and some columns to extract into a smaller matrix:
>> M = magic(4) %# Create a 4-by-4 matrix M = 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1 >> rowIndex = [1 3]; %# A set of row indices >> columnIndex = []; %# A set of column indices, which happen to be empty >> subM = M(rowIndex,columnIndex) subM = Empty matrix: 2-by-0
Note that the empty result still tells you some information, specifically that you tried to index 2 rows from the original matrix. If the result just showed []
, you wouldn't know if it was empty because your row indices were empty, or your column indices were empty, or both.
There are some cases when an empty matrix defined as []
(i.e. all of its dimensions are 0) may give you different results than an empty matrix that still has some non-zero dimensions. For example, matrix multiplication can give you different (and somewhat non-intuitive) results when dealing with different kinds of empty matrices. Let's consider these 3 empty matrices:
>> a = zeros(1,0); %# A 1-by-0 empty matrix >> b = zeros(0,1); %# A 0-by-1 empty matrix >> c = []; %# A 0-by-0 empty matrix
Now, let's try multiplying these together in different ways:
>> b*a ans = [] %# We get a 0-by-0 empty matrix. OK, makes sense. >> a*b ans = 0 %# We get a 1-by-1 matrix of zeroes! Wah?! >> a*c ans = Empty matrix: 1-by-0 %# We get back the same empty matrix as a. >> c*b ans = Empty matrix: 0-by-1 %# We get back the same empty matrix as b. >> b*c ??? Error using ==> mtimes Inner matrix dimensions must agree. %# The second dimension of the first %# argument has to match the first %# dimension of the second argument %# when multiplying matrices.
Getting a non-empty matrix by multiplying two empty matrices is probably enough to make your head hurt, but it kinda makes sense since the result still doesn't really contain anything (i.e. it has a value of 0).
When concatenating matrices, the common dimension has to match.
It's not currently an error if it doesn't match when one of the operands is empty, but you do get a nasty warning that future versions might be more strict.
Examples:
>> [ones(1,2);zeros(0,9)] Warning: Concatenation involves an empty array with an incorrect number of columns. This may not be allowed in a future release. ans = 1 1 >> [ones(2,1),zeros(9,0)] Warning: Concatenation involves an empty array with an incorrect number of rows. This may not be allowed in a future release. ans = 1 1
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