- Once you know, that
a
is free of zero values, usingnnz(a)
spares you 2 chars compared tonumel(a)
.Once you know, that
a
is free of zero values, usingnnz(a)
spares you 2 chars compared tonumel(a)
. - Prefer
a(a==0)
toa(find(a==0))
.Prefer
a(a==0)
toa(find(a==0))
. ~t
is shorter thant==0
, and even~~t
is shorter thant!=0
.~t
is shorter thant==0
, and even~~t
is shorter thant!=0
.0*(1:n)
is shorter thanzeros(1,n)
0*(1:n)
is shorter thanzeros(1,n)
- Generally,
||
and&&
, unlike many other operators, scalarize the result when the first argument is a scalar. For matrices, only non-empty matrices without elements equal to zero have the logical value of true.Generally,
||
and&&
, unlike many other operators, scalarize the result when the first argument is a scalar. For matrices, only non-empty matrices without elements equal to zero have the logical value of true.Hence, we can do
0||m
instead ofall(all(m))
for any matrix.Try with
0||[1 1;1 0]
and0||[1 1;1 1]
to convince yourself. When you are using a builtin a number of times, do a function handle to spare characters eg.
f=@find
. For short function names at least 3 occurrences justify this, for long ones - even with two occurrences.When a function is a single statement, prefer
f=@(n)dosomething(n)
notation tofunction r=f(n)r=dosomething(n);end
one.Unfortunately, global variables have to be declared both in global scope and in each function using them. But there is an exception: anonymous
@(n)...
functions "see" all variables from the scope where they are called from.It's possible to do
f(a=0,b=0)
instead ofa=0;b=0;f(a,b)
.This seems undocumented feature, but the order of evaluation is from left to right (checked at v. 3.8.1), you can do
zeros(a=n,b=a*a)
to both create a n x n^2 matrix and store it's row and column number ina
andb
variables.The operator precedence table is your friend. Don't do
b=(a==0)
sinceb=a==0
is the same.
Hence, we can do 0||m
instead of all(all(m))
for any matrix.
Try with 0||[1 1;1 0]
and 0||[1 1;1 1]
to convince yourself.
When you are using a builtin a number of times, do a function handle to spare characters eg.
f=@find
. For short function names at least 3 occurences justify this, for long ones - even with two occurences.When a function is a single statement, prefer
f=@(n)dosomething(n)
notation tofunction r=f(n)r=dosomething(n);end
one.Unfortunately, global variables have to be declared both in global scope and in each function using them. But there is an exception: anonymous
@(n)...
functions "see" all variables from the scope where they are called from.It's possible to do
f(a=0,b=0)
instead ofa=0;b=0;f(a,b)
.This seems undocumented feature, but the order of evaluation is from left to right (checked at v. 3.8.1), you can do
zeros(a=n,b=a*a)
to both create a n x n^2 matrix and store it's row and column number ina
andb
variables.The operator precedence table is your friend. Don't do
b=(a==0)
sinceb=a==0
is the same.