# My wishlist for the next version of Julia

As a Matlab person who recently switched over to Julia, below are some items on my wish list. Many thanks for your consideration!

1. Creating a m x n sized NaN should be as simple as NaN(m, n). Right now, I have to do this:
`A = fill!(Array{Float64}(undef, m, n), NaN);`

2. If I have two 1-column data with the same size, A and B, I should be able to get an Index by `Ind = A>3 && B<15`. Right now, I would have to use `map` function to broadcast first:

``````Ind = map(A, B) do a, b
a>3 && b<15;
end
``````
1. I should be able to initialize an array as `A=[]`, and then append to it as simple as `A = [A; b];` Right now, Julia will complain that I can not append a 0 sized array with a m x n sized array.

2. Automatically handle the conversion between vector and array for Index values, instead of relying on users to decide when to use `A` and when to use `vec(A)`.

3. Function should allow us to define the outcome of the function like: `function [A, B] = f1(x, y, z)`, instead of requiring us to use `return` at the end of the function to return the results.

4. I should be able to use a `sortrows` function, by `B = sortrows(A, Index).` Instead of having to find the Index using `sortperm` first.

2 Likes
``````julia> fill(NaN,(3,4))

3×4 Matrix{Float64}:
NaN  NaN  NaN  NaN
NaN  NaN  NaN  NaN
NaN  NaN  NaN  NaN

``````

You are lucky, that will be possible in V1.7 already:

``````julia> A = rand(3,4); B = rand(3,4);

julia> inds =  A .> 0.5 .&& B .> 0.5
3×4 BitMatrix:
0  0  0  0
0  1  1  0
0  0  1  1

``````

Currently you can already do this:

``````julia> A = rand(3,4); B = rand(3,4);

julia> (A .> 0.5) .& (B .> 0.5)
3×4 BitMatrix:
0  0  0  0
0  1  1  1
0  0  1  0

``````

(and I don’t see any immediate harm in that, with the parenthesis, maybe someone can correct me)

If you are just pushing to `A` for the first time, just do `A = b`.

The difference between vectors and matrices is a feature, actually, and the old julia and matlab users have discussed many times the advantages of that here.

that can be done:

``````julia> f(x) = 2
f (generic function with 1 method)

julia> f(1)
2

``````

(but you need to learn the syntax first).

I’m not sure what you want to do here, but isn’t this just this? (edit: or more likely, one of the options here: Sort matrix based on the elements of a specific column - #5 by nicolas - a function that sorts the matrix as a function of one row may exist, I’m not sure)

``````julia> A = rand(3,4);

julia> sort(A, dims=2)
3×4 Matrix{Float64}:
0.0518517  0.055554  0.248347  0.992392
0.168324   0.318615  0.857768  0.864286
0.0809794  0.541194  0.724404  0.911751

``````
27 Likes

You meant `A = copy(b)`, right? Because this is what have more similar semantics with `[A; b]` (when `A` is empty).

2 Likes

Many thanks for sharing these tricks!

5 Likes

Some additional comments which haven’t been covered already. For what it’s worth I have in the past used Matlab extensively, although I switched away many years ago.

You can concatenate an empty matrix if the other dimensions match, e.g. `[zeros(0, 5); zeros(4, 5)]`. But when it comes to the general pattern of repeatedly concatenating to a matrix, that is a pattern which is frequently used in Matlab in lack of better options. Chances are that you will find functions like `push!`, `append!`, and `reduce` to provide satisfactory alternatives.

That is something of an oddball syntax in Matlab, which is different from any other programming language I’ve used and one I’ve never understood the attraction of. Naturally tastes vary but I’m confident that this is never going to be considered based on PSA: Julia is not at that stage of development anymore.

10 Likes

I just tested sort(A, dims=1). All rows are mismatched now. Be very careful about using it.

A =
8 8 6
4 8 7
3 7 3
10 10 1
9 5 5

sort(A, dims=1) gives me the below:
3 5 1
4 7 3
8 8 5
9 8 6
10 10 7

@GunnarFarneback I’m curious, this is proper syntax in Matlab? What does it mean?

1 Like

From matlab’s webpage:

`function [y1,...,yN] = myfun(x1,...,xM)` declares a function named `myfun` that accepts inputs `x1,...,xM` and returns outputs `y1,...,yN` . This declaration statement must be the first executable line of the function. Valid function names begin with an alphabetic character, and can contain letters, numbers, or underscores.

So in their example:

``````function [m,s] = stat(x)
n = length(x);
m = sum(x)/n;
s = sqrt(sum((x-m).^2/n));
end
``````

[m,s] is a way of indicating which of variables in the body of the function should be returned to the caller.
I always found this Matlab syntax ugly.

10 Likes

It’s used when defining functions to specify what variables are returned.

In MATLAB:

``````function [A, B] = f1(x, y, z)

A = % something
B = % something else

end % no return statement, A and B are returned
``````

In Julia:

``````function f1(x, y, z)

A = # something
B = # something else
return (A, B)

end
``````

I think it’s appealing to see both the function signature and what it returns in one line (particularly when more descriptive names are used than just `A` and `B`). Many people do this in docstrings in Julia, e.g., `f1(x, y, z) -> A, B`, or something similar.

6 Likes

`sort(A, dims=1)` sorts each column separately; it is not equivalent to Matlab’s `sortrows`. The closest analogue of `sortrows(A)` from Matlab is `sortslices(A, dims=1)` in Julia.

11 Likes

Aside from the wonderful suggestions of the community, I’ve moved the thread out out “Internals & Design” to “General Usage”, since most of the questions in your list are already quite possible, except that they use different syntax than Matlab. To that regard, please note that the general feel of julia is done and probably won’t change much in the feature (though genuinely new feature requests are of course still welcome!)

6 Likes

What exactly is the difference between

and

if any?

1 Like

The precedence of the operators, importantly, in this case:

``````julia> 2 > 1 && 4 > 3
true

julia> 2 > 1 & 4 > 3
false

julia> (2 > 1) & (4 > 3)
true

``````

(`&` and `&&` are not the same thing)

4 Likes

The one in the middle is so confusing. How come the result is false?

It checks `1 & 4` first, and it is a Bitwise operation - Wikipedia, which returns `0` in this comparison. You get the expected `and` and `or` behavior only if comparing to booleans (which is what the parenthesis guarantee there).

7 Likes

I should point out that you can write `return A,B` or just

``````A, B
``````

if the OP dislikes `return` keyword so much.

1 Like

The syntax closest to Matlab’s `[a,b] = f(x,y)` is probably

``````julia> f(x,y) = (2x+y, 4y^x)
f (generic function with 1 method)

julia> (a,b) = f(2,3)
(7, 36)
``````
1 Like

Well, yes, but this wasn’t about calling a function but defining it.

I see. OP wants

``````function [a,b] = f(x,y)
a = 2x+y
b = 4y^x
end
``````

Not an improvement, IMHO.

6 Likes

In Matlab, an array of objects is returned: hence `[a, b] = ...`. In Julia, a tuple is returned.
So it would read `(a, b) <- f(x) = (x, 2*x)`, perhaps?

I do appreciate the information available from the listing of the arguments in the place where the function begins to be defined. However, in Matlab this is then spoiled immediately since the named outputs can be assigned value anywhere inside the function! Not easy to read.

6 Likes