comparison toolboxes/FullBNT-1.0.7/KPMtools/assign_cols.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e9a9cd732c1e
1 function M = assign_cols(cols, vals, M)
2 % ASSIGN_COLS Assign values to columns of a matrix
3 % function M = assign_cols(M, cols, vals, M)
4 %
5 % Example:
6 % M = assign_cols(data, ones(1,N))
7 % will construct a 1-of-K encoding of the data, where K=ncols=max(data) and N=nrows=length(data)
8 %
9 % Example:
10 % M = zeros(3,2);
11 % M = assign_cols([1 2 1], [10 20 30], M)
12 % is equivalent to
13 % M(1, 1) = 10
14 % M(2, 2) = 20
15 % M(3, 1) = 30
16 %
17
18 if nargin < 3
19 nr = length(cols);
20 nc = max(cols);
21 M = zeros(nr, nc);
22 else
23 [nr nc] = size(M);
24 end
25
26 if 0
27 for r=1:nr
28 M(r, cols(r)) = vals(r);
29 end
30 end
31
32 if 1
33 rows = 1:nr;
34 ndx = subv2ind([nr nc], [rows(:) cols(:)]);
35 M(ndx) = vals;
36 end