Borrar filtros
Borrar filtros

What the heck is a 1×0 empty double row vector and why does MATLAB create them?

151 visualizaciones (últimos 30 días)
Try 1:0 and the result is a "1×0 empty double row vector". Why? Why is it not the same as [ ]?
Furthermore, if I have a vector like a=[1 2] and take the logical of it a([false false]), I again get an empty row vector. Why is the row sticking around? MATLAB isn't calling the columns empty. It's not a 1x2 empty vector.
Is this somehow useful in some application? Because I've found tons of places where it's totally not useful at all.
  1 comentario
Torsten
Torsten el 13 de Jun. de 2023
I think you are more frustrated that your code does not give a result than about the kind of object MATLAB returns to show you that your code does not give a result, aren't you ?

Iniciar sesión para comentar.

Respuesta aceptada

Steven Lord
Steven Lord el 13 de Jun. de 2023
Try 1:0 and the result is a "1×0 empty double row vector". Why? Why is it not the same as [ ]?
The output of the colon operator should always be a row vector. That means it always has 1 row. From this alone it means [] can't be the output for consistency's sake, since [] is not a vector and does not have 1 row.
isvector([])
ans = logical
0
size([], 1)
ans = 0
Since you can't get to 0 from 1 in steps of 1 unit, the output should have no elements. That means it's a 1-by-0 vector.
x = 1:0
x = 1×0 empty double row vector
Furthermore, if I have a vector like a=[1 2] and take the logical of it a([false false]), I again get an empty row vector. Why is the row sticking around? MATLAB isn't calling the columns empty. It's not a 1x2 empty vector.
If a([false false]) should have two elements, what should those elements be? They can't be either 1 or 2, as the corresponding logical indices being false means they should not be included in the result of that indexing operation. But all the elements resulting from an indexing operation should be elements of the original array, right? So that means the elements of that result can't be anything but 1 or 2.
Or do you mean that you believe we should return a 0-by-0 empty?
M = [1; 2; 3];
M([true; true; true]) % Size [3 1]
ans = 3×1
1 2 3
M([true; true; false]) % Size [2 1]
ans = 2×1
1 2
M([true; false; false]) % Size [1 1]
ans = 1
M([false; false; false])
ans = 0×1 empty double column vector
Why should the last of those lines break the pattern and return a result of size [0 0] instead of a result of size [0 1]? Edge cases are annoying because all too often they require you to do something special in your code that uses the code with the edge cases.
Is this somehow useful in some application? Because I've found tons of places where it's totally not useful at all.
Long ago, the only empty array in MATLAB was the 0-by-0 matrix. People wrote scholarly papers (linked in Professor Nick Higham's blog post about empty matrices in MATLAB) about how this was "neither correct, consistent, or useful".
In general, not just in indexing operations, empties of sizes other than 0-by-0 are useful in places like concatenation. In the example below, in order to form M the array C will need to have the same number of rows as A and the same number of columns as B. Otherwise the pieces don't "fit" together. For this particular A and B, that means C has to be 2-by-0.
A = [1 2; 3 4];
B = [];
C = zeros(size(A, 1), size(B, 2))
C = 2×0 empty double matrix
M = [A, C; C.', B]
M = 2×2
1 2 3 4
You can see this pattern if you change B.
A = [1 2; 3 4];
for szB = 4:-1:0
B = ones(szB);
fprintf("szB is %d\n", szB)
C = zeros(size(A, 1), size(B, 2))
M = [A, C; C.', B]
end
szB is 4
C = 2×4
0 0 0 0 0 0 0 0
M = 6×6
1 2 0 0 0 0 3 4 0 0 0 0 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1
szB is 3
C = 2×3
0 0 0 0 0 0
M = 5×5
1 2 0 0 0 3 4 0 0 0 0 0 1 1 1 0 0 1 1 1 0 0 1 1 1
szB is 2
C = 2×2
0 0 0 0
M = 4×4
1 2 0 0 3 4 0 0 0 0 1 1 0 0 1 1
szB is 1
C = 2×1
0 0
M = 3×3
1 2 0 3 4 0 0 0 1
szB is 0
C = 2×0 empty double matrix
M = 2×2
1 2 3 4

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by