Skip to content
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

Closed
alexdubbs opened this issue Jul 22, 2011 · 9 comments
Closed

Should functions treat 1xn arrays as vectors or matrices? #142

alexdubbs opened this issue Jul 22, 2011 · 9 comments

Comments

@alexdubbs
Copy link

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.

@ViralBShah
Copy link
Member

sort needs to be implemented for slices of arrays. Just hasn't been done yet. This is where sub() will be useful.

@JeffBezanson
Copy link
Sponsor Member

Whether something is a slice is different from whether it is 1xn. We can add a case like we did for diagm.

@ViralBShah
Copy link
Member

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:

Whether something is a slice is different from whether it is 1xn. We can add a case like we did for diagm.

Reply to this email directly or view it on GitHub:
#142 (comment)

@JeffBezanson
Copy link
Sponsor Member

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.

@ViralBShah
Copy link
Member

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.

@JeffBezanson
Copy link
Sponsor Member

I agree. To sort a 1xn like a vector you should reshape or slice it or use
sort (x,2). Then sort (x) can consistently default to sort (x,1).

@ViralBShah
Copy link
Member

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.

@JeffBezanson
Copy link
Sponsor Member

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.
We can add an entry to the differences from matlab doc section.

@JeffBezanson
Copy link
Sponsor Member

Decided. We're going to be consistent.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants