Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
hendri54 committed Jan 11, 2016
1 parent 82aaba7 commit 1f31f89
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 23 deletions.
47 changes: 43 additions & 4 deletions matlab_datatypes.mmd
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@ These are really vectors of characters to Matlab.

Then there are more specialized datatypes such as `tables`.

You can also define your own data types. We will talk about that more later (object oriented programing).

The type of a variable is **not fixed**.

```matlab
a = 1;
a = 'a string';
```

is perfectly fine code (and a popular source of errors).

##Vectors
## Vectors

To create a vector, simply fill it with values:

Expand All @@ -43,15 +47,31 @@ Matlab knows row and column vectors:
14
```

###The colon operator
### The colon operator

To extract elements from a vector, use the colon:
The colon `:` makes a vector of sequential numbers:

```matlab
disp(1 : 4);
1 2 3 4
% Now with step size 3
disp(1 : 3 : 10);
1 4 7 10
```

### Indexing ###

To extract elements from a vector, hand it a list (vector) of indices.

```matlab
>> a = [2, 3, 4, 5];
>> disp(a[1, 3]);
2 4
```

The vector of indices can be constructed with the colon operator:

```matlab
a = 11 : 20; disp(a(2 : 2 : 6));
12 14 16
```
Expand All @@ -63,7 +83,22 @@ Any vector can be used to index elements:
12 15 18
```

##Matrices
Then there is [logical indexing](http://blogs.mathworks.com/steve/2008/01/28/logical-indexing/).

```matlab
>> a = 11 : 20; idxV = (a > 15);
>> disp(idxV)
0 0 0 0 0 1 1 1 1 1
>> disp(a(idxV))
16 17 18 19 20
```

See also the [find](http://www.mathworks.com/help/matlab/ref/find.html) function for a similar idea.

## Matrices

A matrix is an n-dimensional array of numbers.
One can also have arrays of other data types (see [cell arrays](#cell-arrays)).

To create a matrix, simply fill it with values.

Expand Down Expand Up @@ -227,6 +262,10 @@ Example:
Integer: 5. Float: 3.71. String: test
```

## Cell Arrays ##

(http://www.mathworks.com/help/matlab/cell-arrays.html)

## Numeric Precision ##

By default, numeric variables are stored as `double` (double precision float, 64 bit).
Expand Down
15 changes: 8 additions & 7 deletions matlab_intro.mmd
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# Matlab: Introduction #

[link](matlab_exercises.html#growth-model)

## Installation and Configuration

Instructions for installation from [ITS](http://software.sites.unc.edu/software/)
Expand Down Expand Up @@ -50,10 +48,12 @@ It is easy to learn and use.

* The main reason is that Matlab is *dynamically typed*. That is, a variable can change type as it is modified.
* Arguments are passed to functions without declaring their types.
* The documentation is outstanding.

Matlab has a large standard library.

* But there is little (high quality) user contributed code.
* Most of it lives on the [Matlab file exchange](http://www.mathworks.com/matlabcentral/fileexchange/). But this is not curated in any way. Use at your own risk.

### Matlab Cons ##

Expand All @@ -67,9 +67,9 @@ In the command window, type your commands at the `>>` prompt.

Matlab returns the answers on the screen.

####Example
**Example**

date
>> date
ans =
26-Jul-2015

Expand All @@ -83,13 +83,14 @@ Its return value is shown on the screen after `ans =`.

To get rid of the `ans =` part, we could use `disp(date)`.

####Example
**Example**

dateString = date;
disp(dateString);
>> dateString = date;
>> disp(dateString);
26-Jul-2015

>Note: Matlab displays the result of any command, unless the command is ended with `;`.

>If you ever see a lot of numbers appearing on the screen "out of nowhere," you probably forgot the `;` at the end of a line.

##Doing Math
Expand Down
26 changes: 14 additions & 12 deletions programming.mmd
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Write your code [*top down by stepwise refinement*](http://www.eecs.wsu.edu/~cs1

* Start with an outline of the steps.
* For each step:
* if it's trivial: write it out in *pseudo code*.
* if it's trivial: write it out in [pseudo code](https://en.wikipedia.org/wiki/Pseudocode).
* it it's not: write another outline for that step
* Finally, translate the pseudo code into code.

Expand All @@ -35,14 +35,14 @@ Read a good book on best practices in programming.
1. I see a lot of very poorly written code that is impossible to understand and not robust. Do yourself a favor and save a lot of time down the road by learning to how write quality code.
2. A book I like is "Writing Solid Code."

##Some rules
## Some rules

1. No literals as in "x=zeros([5,3])" or for "i1 = 1 : 57". It's not robust and hard to read.
1. No literals as in `x=zeros([5,3])` or for `i1 = 1 : 57`. It's not robust and hard to read.
2. No global variables.
3. Don't worry about speed. Worry about robustness and transparency.
4. Unique names: I suffix all functions I write with a project code (e.g. var_load_sc.m, var_save_sc.m, etc). It avoids naming conflicts with other projects.
4. Unique names: I suffix all functions I write with a project code (e.g. `var_load_sc.m`, `var_save_sc.m`, etc). It avoids naming conflicts with other projects.
5. Your code should contain lots of self-testing code. Most code is so fast that the loss of speed is irrelevant. If it is relevant, have a  switch that globally switches test code on and off.
6. Avoid using reserved words, in particular "i" as an index.
6. Avoid using reserved words, in particular `i` as an index.

### Style matters

Expand Down Expand Up @@ -77,11 +77,13 @@ Related to this: do not hard-code *functional forms*.

If you want to compute the marginal product of capital, write a function for it. Otherwise, if you want to switch from Cobb-Douglas to CES, you have to rewrite all your programs.

* [Object oriented programming](https://en.wikipedia.org/wiki/Object-oriented_programming) makes it easy to swap out entire parts of a model. We will talk about this later.

### Self-Test Code ##

Your code should test itself automatically and periodically.

Embed error catching code everywhere (`validateattributes`).
Embed error catching code everywhere (use [valideattributes](http://www.mathworks.com/help/matlab/ref/validateattributes.html)).

Catching bugs early makes them easier to find.

Expand Down Expand Up @@ -119,13 +121,13 @@ It turns out that it spends 80% of its time running the Matlab interpolation fun

There is little point optimizing the rest of the code.

To find out what makes your program slow, run the Matlab **Profiler** (help profile).
To find out what makes your program slow, run the Matlab [profiler](http://www.mathworks.com/help/matlab/ref/profile.html).

Some of Matlab's built-in functions are extremely slow.

Two examples are `interp1` and `sub2ind`.

It is easy to write replacements that run ten times faster.
* Two examples are `interp1` and `sub2ind`.
* It is easy to write replacements that run ten times faster.
* The [Lightspeed](http://research.microsoft.com/en-us/um/people/minka/software/lightspeed/) library contains faster versions of built-in functions.

## Common mistakes

Expand All @@ -144,7 +146,7 @@ To avoid this: check that inputs have admissible values.
Matlab permits to omit input or output arguments when calling a function.

It is useful to check that the number of input arguments is as expected
using `nargin}.
using [nargin](http://www.mathworks.com/help/matlab/ref/nargin.html).

### Reusing variable names.

Expand All @@ -158,7 +160,7 @@ especially true for code that wraps a loop into a single line of code.

For example, this is easy to read:

````
````matlab
for ix = 1 : nx
zV(ix) = xV(ix+2) + yV(nx + 2 - ix);
end
Expand Down

0 comments on commit 1f31f89

Please sign in to comment.