Highlights
Follow


Poll is CLOSED

Poll

Which of the following does not return the correct length (5) of v = [6,2,9,7,3]?

numel(v)
6%
length(v)
13%
width(v)
14%
nnz(v)
8%
size(v, 1)
27%
sum(v > 0)
31%
2537 votes

John D'Errico
John D'Errico on 14 May 2024
I'll argue that I terribly dislike like several of the "answers". For example,
sum(v>0)
is problematic, since it fails to produce the correct result if one or more of the elements is less than or equal to zero.
nnz(v)
fails for a similar reason in my eyes. A solution that solves a problem only for specific cases is a bad one, since one day, that code will fail and be difficult to debug. Perhaps someone might argue that
sum(v | ~v)
might have been a better choice. That succeeds as long as none of the elements of v are NaN.
Anyway, a poll like this should help teach newer users what good, robust code looks like, insted of convincing at least one new user out there that sum(v>0) might be an adequate replacement for numel(z).
goc3
goc3 on 14 May 2024
Thank you for your comment, John—your insights and experience are appreciated. Aside from the fact that this question refers to a specific vector that contains only positive integers, you do raise some helpful points. I would also mention that poll questions and options/answers have rather small character limits.
That being said, I agree that a novice could misinterpret some of the options as being viable replacements. I would argue that some of them are viable in specific contexts and/or for specific purposes. However, the main point of these polls is to catch a user's attention and make them think—perhaps even causing them to open up the documentation or test out the options. There are many built-in functions that overlap and can be interchanged for specific use cases.
bhagath
bhagath on 3 May 2024
ll[][]][[[][;
Josh
Josh on 22 Apr 2024
thank you for continuing with these!
Deepu
Deepu on 22 Apr 2024
  • numel(v): Returns the number of elements in the array v. In this case, it correctly returns 5.
  • length(v): Returns the number of elements along the longest dimension of the array v. Since v is a vector, length(v) correctly returns 5.
  • width(v): There is no built-in function named width in MATLAB. Therefore, this option is incorrect.
  • nnz(v): Returns the number of non-zero elements in the array v. Since all elements in v are non-zero, nnz(v) correctly returns 5.
  • size(v, 1): Returns the size of the array v along the specified dimension. Since v is a row vector, size(v, 1) correctly returns 1.
  • sum(v > 0): Returns the sum of elements in the logical array resulting from the condition v > 0. Since all elements in v are greater than 0, sum(v > 0) correctly returns 5.
Mario Malic
Mario Malic on 22 Apr 2024
This is an AI chat playground (or some other) generated answer. I suspected it as it says "correctly returns 1" and "correctly returns 5".
I asked the same question as well, and it said that width doesn't give the correct answer, but it did not say the reason.
Stephen23
Stephen23 on 3 May 2024 (Edited on 3 May 2024)
Unfortunately users seem to take whatever AI gives them, without any basic cursory check themselves, e.g. reading the documentation or running some simple code. This is the fraud of contemporary AI: promised as another tool in our toolboxes (which is a valid perspective), but ends up with users accepting its output without critical understanding or review (which should not occur with any tool).
goc3
goc3 on 22 Apr 2024
A big shortcoming of AI is being trained on out-of-date data. As DGM pointed out below, width() has been modified over time.
DGM
DGM on 22 Apr 2024
width() was introduced in R2013b, though it did not operate on numeric arrays until R2020b. See the warning note at the bottom of the doc page.
Consequently, depending on your version, there are two cases where a result of 5 is not returned.
Deepu
Deepu on 23 Apr 2024
The two cases where a result of 5 is not returned, depending on the MATLAB version, are:
  1. For versions earlier than R2013b: The width() function was not introduced until R2013b, so attempting to use it in earlier versions would result in an error. In this case, the code would fail to execute, and no result would be returned.
  2. For versions between R2013b and R2020b: While the width() function was introduced in R2013b, it did not operate on numeric arrays until R2020b. Therefore, in versions between R2013b and R2020b, using width() on numeric arrays would result in an error or unexpected behavior. The code may execute, but the result may not be accurate or consistent with the expected behavior.
In both cases, if the width() function is used on numeric arrays, it may not return the correct length of 5, as specified in the question.
DGM
DGM on 23 Apr 2024 (Edited on 23 Apr 2024)
Where are you getting the idea that width() will will return inaccurate or unexpected results in R2013b-R2020b?
In all versions prior to R2020b, if width() is fed a numeric argument, it will throw the same "undefined function or method" error, though the exact wording has changed slightly over the years.
The same is true for height()
No, the "two cases" are not "older than R2013b" and "between R2013b and R2020b". They're choices 3 and 5.
Deepu
Deepu on 23 Apr 2024
I appreciate your clarification regarding the behavior of the width() function in MATLAB versions prior to R2020b. You're correct that in versions before R2020b, width() will throw an "undefined function or method" error when fed a numeric argument, similar to the behavior of height().
Regarding the "two cases" mentioned in the context of the original question, you rightly point out that they are choices 3 and 5, rather than versions of MATLAB. Thank you for providing clarity on this matter.
I apologize if my previous response caused confusion. My intention was to explain the potential reasons why width() may not return the correct length in MATLAB versions prior to R2020b.
DGM
DGM on 23 Apr 2024
I'll tone it down a bit. Sometimes I forget that everybody here isn't already used to me being an ass.
AC
AC on 3 May 2024
For what it's worth, you're likely conversing with an AI (as Mario pointed out above). This is the exact phrasing that chatGPT 3/4 uses.
Deepu
Deepu on 23 Apr 2024
Thank you for acknowledging that. It's important to ensure that our interactions are respectful and considerate of everyone's feelings. Let's work together to foster a positive and supportive environment for everyone here.

See Also

Tags

No tags entered yet.