Mercurial > hg > camir-aes2014
annotate toolboxes/FullBNT-1.0.7/docs/cellarray.html @ 0:e9a9cd732c1e tip
first hg version after svn
author | wolffd |
---|---|
date | Tue, 10 Feb 2015 15:05:51 +0000 |
parents | |
children |
rev | line source |
---|---|
wolffd@0 | 1 Cell arrays are a little tricky in Matlab. |
wolffd@0 | 2 Consider this example. |
wolffd@0 | 3 |
wolffd@0 | 4 C=num2cell(rand(2,3)) |
wolffd@0 | 5 C = |
wolffd@0 | 6 [0.4565] [0.8214] [0.6154] |
wolffd@0 | 7 [0.0185] [0.4447] [0.7919] |
wolffd@0 | 8 |
wolffd@0 | 9 C{1,2} % this is the contents of this cell (could be a vector or a string) |
wolffd@0 | 10 ans = |
wolffd@0 | 11 0.8214 |
wolffd@0 | 12 |
wolffd@0 | 13 C{1:2,2} % this is the contents of these cells - returns multiple |
wolffd@0 | 14 answers! |
wolffd@0 | 15 ans = |
wolffd@0 | 16 0.8214 |
wolffd@0 | 17 ans = |
wolffd@0 | 18 0.4447 |
wolffd@0 | 19 |
wolffd@0 | 20 A = C(1:2,2) % this is a slice of the cell array |
wolffd@0 | 21 ans = |
wolffd@0 | 22 [0.8214] |
wolffd@0 | 23 [0.4447] |
wolffd@0 | 24 |
wolffd@0 | 25 A{1} % A is itself a cell array |
wolffd@0 | 26 ans = |
wolffd@0 | 27 0.8214 |
wolffd@0 | 28 |
wolffd@0 | 29 |
wolffd@0 | 30 |
wolffd@0 | 31 >> C(1:2,2)=0 % can't assign a scalar to a cell array |
wolffd@0 | 32 C(1:2,2)=0 |
wolffd@0 | 33 ??? Conversion to cell from double is not possible. |
wolffd@0 | 34 |
wolffd@0 | 35 |
wolffd@0 | 36 >> C(1:2,2)={0;0} % can assign a cell array to a cell array |
wolffd@0 | 37 C(1:2,2)={0;0} |
wolffd@0 | 38 C = |
wolffd@0 | 39 [0.4565] [0] [0.6154] |
wolffd@0 | 40 [0.0185] [0] [0.7919] |
wolffd@0 | 41 |
wolffd@0 | 42 |
wolffd@0 | 43 BTW, I use cell arrays for evidence for 2 reasons: |
wolffd@0 | 44 |
wolffd@0 | 45 1. [] indicates missing values |
wolffd@0 | 46 2. it can easily represent vector-valued nodes |
wolffd@0 | 47 |
wolffd@0 | 48 The following example makes this clear |
wolffd@0 | 49 |
wolffd@0 | 50 C{1,1} = [] |
wolffd@0 | 51 C{2,1} = rand(3,1) |
wolffd@0 | 52 |
wolffd@0 | 53 C = |
wolffd@0 | 54 [] [0] [0.6154] |
wolffd@0 | 55 [3x1 double] [0] [0.7919] |
wolffd@0 | 56 |
wolffd@0 | 57 |
wolffd@0 | 58 Hope this helps, |
wolffd@0 | 59 Kevin |