Skip to main content
Fixed list numbering by appropriately indenting the additional paragraphs under point 5. Manually renumbering was just to meet the minimum change requirement.
Source Link
  1. Once you know, that a is free of zero values, using nnz(a) spares you 2 chars compared to numel(a).

    Once you know, that a is free of zero values, using nnz(a) spares you 2 chars compared to numel(a).

  2. Prefer a(a==0) to a(find(a==0)).

    Prefer a(a==0) to a(find(a==0)).

  3. ~t is shorter than t==0 , and even ~~t is shorter than t!=0.

    ~t is shorter than t==0 , and even ~~t is shorter than t!=0.

  4. 0*(1:n) is shorter than zeros(1,n)

    0*(1:n) is shorter than zeros(1,n)

  5. 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 of all(all(m)) for any matrix.

    Try with 0||[1 1;1 0] and 0||[1 1;1 1] to convince yourself.

  6. 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.

  7. When a function is a single statement, prefer f=@(n)dosomething(n) notation to function r=f(n)r=dosomething(n);end one.

  8. 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.

  9. It's possible to do f(a=0,b=0) instead of a=0;b=0;f(a,b).

  10. 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 in a and b variables.

  11. The operator precedence table is your friend. Don't do b=(a==0) since b=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.

  1. 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.

  2. When a function is a single statement, prefer f=@(n)dosomething(n) notation to function r=f(n)r=dosomething(n);end one.

  3. 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.

  4. It's possible to do f(a=0,b=0) instead of a=0;b=0;f(a,b).

  5. 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 in a and b variables.

  6. The operator precedence table is your friend. Don't do b=(a==0) since b=a==0 is the same.

  1. Once you know, that a is free of zero values, using nnz(a) spares you 2 chars compared to numel(a).
  2. Prefer a(a==0) to a(find(a==0)).
  3. ~t is shorter than t==0 , and even ~~t is shorter than t!=0.
  4. 0*(1:n) is shorter than zeros(1,n)
  5. 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 of all(all(m)) for any matrix.

Try with 0||[1 1;1 0] and 0||[1 1;1 1] to convince yourself.

  1. 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.

  2. When a function is a single statement, prefer f=@(n)dosomething(n) notation to function r=f(n)r=dosomething(n);end one.

  3. 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.

  4. It's possible to do f(a=0,b=0) instead of a=0;b=0;f(a,b).

  5. 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 in a and b variables.

  6. The operator precedence table is your friend. Don't do b=(a==0) since b=a==0 is the same.

  1. Once you know, that a is free of zero values, using nnz(a) spares you 2 chars compared to numel(a).

  2. Prefer a(a==0) to a(find(a==0)).

  3. ~t is shorter than t==0 , and even ~~t is shorter than t!=0.

  4. 0*(1:n) is shorter than zeros(1,n)

  5. 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 of all(all(m)) for any matrix.

    Try with 0||[1 1;1 0] and 0||[1 1;1 1] to convince yourself.

  6. 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.

  7. When a function is a single statement, prefer f=@(n)dosomething(n) notation to function r=f(n)r=dosomething(n);end one.

  8. 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.

  9. It's possible to do f(a=0,b=0) instead of a=0;b=0;f(a,b).

  10. 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 in a and b variables.

  11. The operator precedence table is your friend. Don't do b=(a==0) since b=a==0 is the same.

Source Link

  1. Once you know, that a is free of zero values, using nnz(a) spares you 2 chars compared to numel(a).
  2. Prefer a(a==0) to a(find(a==0)).
  3. ~t is shorter than t==0 , and even ~~t is shorter than t!=0.
  4. 0*(1:n) is shorter than zeros(1,n)
  5. 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 of all(all(m)) for any matrix.

Try with 0||[1 1;1 0] and 0||[1 1;1 1] to convince yourself.

  1. 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.

  2. When a function is a single statement, prefer f=@(n)dosomething(n) notation to function r=f(n)r=dosomething(n);end one.

  3. 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.

  4. It's possible to do f(a=0,b=0) instead of a=0;b=0;f(a,b).

  5. 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 in a and b variables.

  6. The operator precedence table is your friend. Don't do b=(a==0) since b=a==0 is the same.