What the heck is a 1×0 empty double row vector and why does MATLAB create them?
131 次查看(过去 30 天)
显示 更早的评论
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 个评论
Torsten
2023-6-13
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 ?
采纳的回答
Steven Lord
2023-6-13
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([])
size([], 1)
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
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]
M([true; true; false]) % Size [2 1]
M([true; false; false]) % Size [1 1]
M([false; false; false])
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))
M = [A, C; C.', B]
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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!