Highlights
Verfolgen


Abstimmung ist GESCHLOSSEN

Abstimmung

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 Stimmen

David Cazenave
David Cazenave am 14 Apr. 2024
I say it's A(3,3) = 0 (after the poll closed). Thank you ... thank you very much.
Stephen23
Stephen23 am 19 Mär. 2024
@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))
Jim Riggs
Jim Riggs am 18 Mär. 2024 (Bearbeitet am 18 Mär. 2024)
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.
Dr.GADDALA JAYA RAJU
Dr.GADDALA JAYA RAJU am 15 Mär. 2024
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.
Stephen23
Stephen23 am 19 Mär. 2024 (Bearbeitet am 19 Mär. 2024)
"Let's go through each option"
An authoritative sounding comment ... lets check some of those statements:
  • "Since cos(π/2) is 0..." Note that π is not a valid variable or function name, so this statement is perhaps mathematically true but irrelevant to MATLAB. If we assume that the commenter meant the valid MATLAB code cos(pi/2) then the statement is easily tested: this numeric operation actually results in a value that is very close to zero (but not exactly zero, as others have already commented). Those with some basic understanding of binary floating point numbers will appreciate why this might occur.
  • "A(3, 3) = 0: This will not produce a 3x3 array of zeros." is borderline, because it does rather depend on whether A already exists in that workspace (and hence what class/size it has).... so (as others have already commented) we will assume that A does not exist in the workspace: the OP did not test it in MATLAB before commenting, but you can.
  • "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." MATLAB syntax errors are not probabilistic: either a syntax is invalid or it is not. I strongly recommend to all readers that they try this themselves in MATLAB and see what happens. Tip: in general MATLAB does not require variables to exist before allocating to them.
  • "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." Lets quickly revise some basic mathematics: what happens when we multiply an AxB matrix with a BxC matrix? The inside dimensions "cancel", giving a matrix that has size AxC. Now apply that to this example with 3x1 and 1x3 matrices... what is the predicted output size? Then test it in MATLAB.
goc3
goc3 am 15 Mär. 2024
If A already exists as a variable,
A(3, 3) = 0
will indeed only assign zero to the 3rd row, 3rd column. However, this question assumes that A does not exist. In that case, it will not throw an error. You should try it and see what happens.
It is also true that cos(π/2) is 0. However, see what happens when you run the following code in MATLAB:
cos(pi/2)
A small value may be practically zero for many applications. However, it is not technically equal to zero.
Dominique
Dominique am 14 Mär. 2024
Soory, I read the question incorrectly.
Marco Riani
Marco Riani am 2 Mär. 2024
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
Matt J
Matt J am 29 Feb. 2024 (Bearbeitet am 29 Feb. 2024)
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?
Mario Malic
Mario Malic am 29 Feb. 2024
MATception
Benjamin
Benjamin am 28 Feb. 2024 (Bearbeitet am 28 Feb. 2024)
OK, dumb question: where can I see the correct answer?
the cyclist
the cyclist am 28 Feb. 2024
Open up a fresh instance of MATLAB, run each of the answers, and see which one fails to produce a 3x3 array of zeros.
Adam Danz
Adam Danz am 26 Feb. 2024
Great poll @goc3!
Great way to roundoff the day.
Stephen23
Stephen23 am 5 Mär. 2024
I was just floating past this thread and loved the overflow of inspiration accumulated in these comments. Thanks everyone for the signs of positivity and bits of humor, that really rounded my day up! It is these least significant moments that really matter most :)
Christian Schröder
Christian Schröder am 26 Feb. 2024
And that's why cospi() exists.
Matt J
Matt J am 29 Feb. 2024
Or cosd().
the cyclist
the cyclist am 26 Feb. 2024
Question: What is the point of this poll?
Answer: Floating
Bogdan -Ervin
Bogdan -Ervin am 27 Feb. 2024
Why A(3,3) isn't correct?
the cyclist
the cyclist am 27 Feb. 2024
There is an implicit assumption there that A does not already exist in the workspace. If it doesn't, then
A(3,3) = 0
will indeed create a 3x3 array of zeros.
Mario Malic
Mario Malic am 26 Feb. 2024
Uhh, implicit expansion made me guess wrong😅
the cyclist
the cyclist am 26 Feb. 2024 (Bearbeitet am 26 Feb. 2024)
SPOILER ALERT
SPOILER ALERT
SPOILER ALERT
SPOILER ALERT
SPOILER ALERT
SPOILER ALERT
Interestingly, matrix multiplication and array (i.e. element-wise) multiplcation give the same result here.
mtimes([1;1;0], [0,0,0])
ans =
0 0 0
0 0 0
0 0 0
times([1;1;0], [0,0,0])
ans =
0 0 0
0 0 0
0 0 0

Tags

Noch keine Tags eingegeben.