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

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

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!)

Also, see this thread for a bit more info about this:

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

There is potentially a big difference between Matlab’s `[a,b] = fm(...)`

and Julia’s `a,b = fj(...)`

.

In Matlab it’s possible to have internal conditional code to **not compute** `b`

if the function is called as `a = fm(...)`

. This is not possible in Julia.

5 Likes

Note that & is a bitwise and operator. What is evaluated in `2 > 1 & 4 > 3`

first is `1 & 4`

:

```
julia> 1 & 4
0
```

But note the type of the result

```
julia> typeof(1 & 4)
Int64
```

What is evaluated subsequently is

```
julia> 2 > 0 > 3
false
```

5 Likes

For those who wonder how this works, you have a magical variable `nargout`

in your function, which you can check to see how many outputs the caller will receive, like this:

Similarly you have a `nargin`

variable available for the input arguments, which can be used to assign default values to left out arguments, or other transformations of the arguments, e.g.

https://github.com/GunnarFarneback/spatial_domain_toolbox/blob/5c55d5c4e57197cc9ccc3a6b9a1656dc57ae77dd/polyexp.m#L189-L212

I’m particularly happy about having left that language feature behind me.

11 Likes

Oh no, let’s not go there!

2 Likes

my limited explore of MATLAB has told me that every function has 100 lines preamble to check `nargin`

and `nargout`

which is just… why

10 Likes