Mercurial > hg > ltpda
comparison m-toolbox/classes/+utils/@math/randelement.m @ 0:f0afece42f48
Import.
author | Daniele Nicolodi <nicolodi@science.unitn.it> |
---|---|
date | Wed, 23 Nov 2011 19:22:13 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:f0afece42f48 |
---|---|
1 % RANDELEMENT(VECTOR,J) returns J random samples chosen in the VECTOR array. | |
2 % | |
3 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
4 % | |
5 % DESCRIPTION: RANDELEMENT(VECTOR,N) returns N random samples chosen in the | |
6 % VECTOR array. If VECTOR is a scalar, | |
7 % than it answers with N samples from | |
8 % 1:1:VECTOR. | |
9 % | |
10 % CALL: out = randelement(arr, j) | |
11 % | |
12 % INPUTS: arr - numerical vector array or cell vector array; can be | |
13 % single line or single column. | |
14 % j - number of samples to extract | |
15 % | |
16 % OUTPUTS: out - numerical vector array or cell vector array, shaped as | |
17 % the input 'arr', containing j elements chosen among | |
18 % those included in 'arr'. | |
19 % | |
20 % VERSION: $Id: randelement.m,v 1.5 2009/08/10 00:49:50 nicola Exp $ | |
21 % | |
22 % HISTORY: 06-02-2008 N Tateo | |
23 % Creation | |
24 % | |
25 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
26 | |
27 function out = randelement(arr, j) | |
28 | |
29 if nargin < 2 | |
30 error('randelement:tooFewInputs','The randelement function requires two input arguments.'); | |
31 elseif ~isa(arr,'double') && ~isa(arr,'cell') | |
32 error('randelement:wrongInput','The input to the randelement function must be a numerical or cell array.') | |
33 elseif ~isvector(arr) | |
34 error('randelement:wrongShape','The input to the randelement function must be a vector.') | |
35 end | |
36 | |
37 N = length(arr); | |
38 if N==1 | |
39 if iscell(arr), arr = num2cell([1:1:arr{1}]); | |
40 else arr = [1:1:arr]; | |
41 end | |
42 N = length(arr); | |
43 end | |
44 out = arr(randi(N,1,j)); | |
45 | |
46 if size(arr,1)==1 % reshape as a row vector | |
47 out = reshape(out,1,[]); | |
48 else % reshape as a column vector | |
49 out = reshape(out,[],1); | |
50 end | |
51 | |
52 end | |
53 % END | |
54 |