Poll is CLOSED
Poll
Which of the following will not produce a 3x3 array of zeros in MATLAB?
eye(3) - diag(ones(1,3))
11%
0 ./ ones(3)
9%
cos(repmat(pi/2, [3,3]))
16%
zeros(3)
20%
A(3, 3) = 0
32%
mtimes([1;1;0], [0,0,0])
12%
3009 votes
21 Comments
I say it's A(3,3) = 0 (after the poll closed). Thank you ... thank you very much.
@goc3: thanks for the fun polls :)
The MTIMES example could have been even trickier using empty matrices e.g.:
mtimes(nan(3,0),inf(0,3))
There is an argument why each one of these will prodice a 3x3 array of zeros.
The most popular answer, A(3,3)=0, is a good choice, because it relies on the condition that A does not exist.
For my money, the best answer is cos(repmat(pi/2, [3,3])) - this is because the numerical evaluation of cos(pi/2) is not identically zero, but produces a resut containing 'digital noise'. In my case, I get a 3x3 matrix where each element contains 6.1232e-17.
Let's go through each option:
eye(3) - diag(ones(1,3)): This will produce a 3x3 array of zeros. eye(3) creates a 3x3 identity matrix, and diag(ones(1,3)) creates a diagonal matrix with ones on the diagonal and zeros elsewhere. Subtracting these will result in a 3x3 array of zeros.
0 ./ ones(3): This will produce a 3x3 array of zeros. It divides zero element-wise by a 3x3 matrix of ones, resulting in all elements being zero.
cos(repmat(pi/2, [3,3])): This will not produce a 3x3 array of zeros. repmat(pi/2, [3,3]) creates a 3x3 matrix filled with π/2, and then cos() takes the cosine of each element. Since cos(π/2) is 0, this will indeed produce a 3x3 array of zeros.
zeros(3): This will produce a 3x3 array of zeros. zeros(3) creates a 3x3 matrix filled with zeros.
A(3, 3) = 0: This will not produce a 3x3 array of zeros. This line of code assigns a zero to the element at the 3rd row and 3rd column of matrix A. However, matrix A is not specified in the given options, so this line will likely throw an error unless A has already been defined as a matrix.
mtimes([1;1;0], [0,0,0]): This will not produce a 3x3 array of zeros. This is a matrix multiplication operation between a column vector [1;1;0] and a row vector [0,0,0]. The result will be a scalar, not a 3x3 array of zeros.
So, the option that will not produce a 3x3 array of zeros is option 5: A(3, 3) = 0.eye(3) - diag(ones(1,3)): This will produce a 3x3 array of zeros. eye(3) creates a 3x3 identity matrix, and diag(ones(1,3)) creates a diagonal matrix with ones on the diagonal and zeros elsewhere. Subtracting these will result in a 3x3 array of zeros.0 ./ ones(3): This will produce a 3x3 array of zeros. It divides zero element-wise by a 3x3 matrix of ones, resulting in all elements being zero.cos(repmat(pi/2, [3,3])): This will not produce a 3x3 array of zeros. repmat(pi/2, [3,3]) creates a 3x3 matrix filled with π/2, and then cos() takes the cosine of each element. Since cos(π/2) is 0, this will indeed produce a 3x3 array of zeros.zeros(3): This will produce a 3x3 array of zeros. zeros(3) creates a 3x3 matrix filled with zeros.A(3, 3) = 0: This will not produce a 3x3 array of zeros. This line of code assigns a zero to the element at the 3rd row and 3rd column of matrix A. However, matrix A is not specified in the given options, so this line will likely throw an error unless A has already been defined as a matrix.mtimes([1;1;0], [0,0,0]): This will not produce a 3x3 array of zeros. This is a matrix multiplication operation between a column vector [1;1;0] and a row vector [0,0,0]. The result will be a scalar, not a 3x3 array of zeros.So, the option that will not produce a 3x3 array of zeros is option 5: A(3, 3) = 0.
Soory, I read the question incorrectly.
Other nice alternative to the possible questions above could be starting from a nilpotent matrix such as
([5 -3 2].*[1; 3; 2])^2
Of course
isequal(([5 -3 2].*[1; 3; 2])^2,zeros(3))
ans =
logical
1
I think it could be a trick question, and that the real answer is zeros(3). It is pretty apparent from the timings below that zeros() doesn't necessarily create anything, or at least not right away.
>> timeit(@()zeros(1e4))
ans =
2.5399e-05
>> timeit(@()ones(1e4))
ans =
0.2189
If zeros() was actually creating anything, why does it take 4 orders of magnitude longer to create a matrix of ones than a matrix of zeros?
OK, dumb question: where can I see the correct answer?
And that's why cospi() exists.
Question: What is the point of this poll?
Answer: Floating
Uhh, implicit expansion made me guess wrong😅