-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Should functions treat 1xn arrays as vectors or matrices? #142
Comments
sort needs to be implemented for slices of arrays. Just hasn't been done yet. This is where sub() will be useful. |
Whether something is a slice is different from whether it is 1xn. We can add a case like we did for |
Yes, but the N-d slice case will automatically handle this, and is easy to implement. Special cases will get avoided as well. In case of diagm, there is no such generalization, and hence we added the case. -viral On 01-Aug-2011, at 3:24 AM, JeffBezanson wrote:
|
Currently all our sort functions are declared to work only on 1d. You might have a slice or array that is 1xnx1, so something there will have to change. I don't think there is a different "slice case" of sort, you just happen to pass a SubArray instead of Array. |
Here, one question that comes up is that if we treat 1xn matrix as a vector, we should sort it, but if we treat it as a matrix, then one has to use sort(a, 2) to sort it. I would prefer to keep things clean. Commit c54f7cc starts implementing sort for arrays. |
I agree. To sort a 1xn like a vector you should reshape or slice it or use |
This question comes up in many other cases, such as max, sum, mean and any function that works on a vector. Matlab users are going to be really confused with this behavior. All the defaults will appear to users as NO-OPs. This is also the kind of decision, which, if taken wrong, can lead to a lot of legacy and baggage later on. I feel treating a 1xn array as a matrix and applying uniform rules is the right thing to do. Perhaps we can provide adequate warnings in the documentation and such. One alternative is treating arrays of type 1 x ... x n x ... x 1, where all leading and trailing dimensions are 1 differently. I don't like these special cases though. |
Yes, we should be consistent. Say you have a matrix where each column is a variable, and the rows are samples (typical). You do sort(M) to sort the samples for each variable. In the case where you have only one sample, matlab scrambles your variables instead of doing a no-op like it should. This seems annoying and dangerous to me. |
Decided. We're going to be consistent. |
Test on julia 0.3, 0.4, and nightly
Co-authored-by: K Pamnany <kpamnany@users.noreply.github.com>
sort(1xn vector) does nothing in julia, whereas in matlab it sorts. Should a 1xn matrix in julia be treated as a vector and always sorted, or should it inherit the behavior for matrices?
This question comes up in many other cases, such as max, sum, mean etc.
The text was updated successfully, but these errors were encountered: