In my (admittedly brief) experience, there have been instances in which the code got uglier because []*[] == [], and it would be possible to write more elegant code if it was defined to be 0 instead.
For an example of such a piece of code, this part is supposed to compute x in L*x=b (L being a lower triangular matrix):
x(1) = b(1)/A(1,1);
for i=2:n
x(i) = (b(i) - A(i,1:i-1)*x(1:i-1))/A(i,i);
end
The problem is that I have to make it by two separate cases. I do not think I am obsessed with code beauty, but this is not pleasant for me.
My question is: is there a way to make this work in a single for loop (not using isempty)? Also, I'd love to know if there is a reason I am unaware of for this particular choice the developers made.
Thank you.
isempty
? There are two places I could see it being checked, either before you current loop or inside the loop. $\endgroup$ – Godric Seer Oct 15 '14 at 15:37ones(2,0)*ones(0,3) == zeros(2,3)
. $\endgroup$ – Doug Lipinski Oct 15 '14 at 18:50x=L\b
. An example where you vectorize the inner of two nested loops seems strange; is there another example you had in mind? The design decision in MATLAB seems to be that an $m$ by $n$ matrix times an $n$ by $k$ matrix is always an $m$ by $k$ matrix, even if some of $n$,$m$,and $k$ are zero. $\endgroup$ – Patrick Sanan Oct 15 '14 at 21:02[]
(which is a $0\times 0$ empty matrix), but only $n\times 0$ and $0\times n$ matrices. These are also different things in Matlab, although in some points the syntax tries to hide it. $\endgroup$ – Federico Poloni Oct 16 '14 at 12:10