IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

MATLAB Discussion :

Error using ==> assert


Sujet :

MATLAB

  1. #1
    Membre éclairé
    Inscrit en
    Septembre 2009
    Messages
    343
    Détails du profil
    Informations forums :
    Inscription : Septembre 2009
    Messages : 343
    Par défaut Error using ==> assert
    Salut, je débute en matlab: j'ai utilisé une fonction prête en matlab et voici le code de cette fonction

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
     
     
    function M = slmetric_pw(X1, X2, mtype, varargin)
    %SLMETRIC_PW Compute the metric between column vectors pairwisely
    %
    % [ Syntax ]
    %   - M = slmetric_pw(X1, X2, mtype);
    %   - M = slmetric_pw(X1, X2, mtype, ...);
    %
    % [ Arguments ]
    %   - X1, X2:       the sample matrices
    %   - mtype:        the string indicating the type of metric
    %   - M:            the resulting metric matrix
    %
    % [ Description ]
    %    - M = slmetric_pw(X1, X2, mtype) Computes the metrics between
    %      column vectors of X1 and X2 pairwisely, using the metric
    %      specified by mtype. 
    %
    %      Both X1 and X2 are matrices with each column representing a 
    %      sample. X1 and X2 should have the same number of rows. Suppose
    %      the size of X1 is d x n1, and the size of X2 is d x n2. Then 
    %      the output metric matrix M will be of size n1 x n2, in which
    %      M(i, j) is the metric value between X1(:,i) and X2(:,j).
    %
    %    - M = slmetric_pw(X1, X2, mtype, ...) Some metric types requires
    %      extra parameters, which should be specified in params.
    %
    %      The supported metrics of this function are listed as follows: 
    %      \{:
    %        - eucdist:        Euclidean distance: 
    %                          $ ||x - y|| $
    %
    %        - sqdist:         Square of Euclidean distance: 
    %                          $ ||x - y||^2 $
    %
    %        - dotprod:        Canonical dot product: 
    %                          $ <x,y> = x^T * y $ 
    %
    %        - nrmcorr:        Normalized correlation (cosine angle):
    %                          $ (x^T * y ) / (||x|| * ||y||) $
    %
    %        - corrdist:       Normalized Correlation distance
    %                          $ 1 - nrmcorr(x, y) $
    %
    %        - angle:          Angle between two vectors (in radian)  
    %                          $ arccos (nrmcorr(x, y)) $
    %        - quadfrm:        Quadratic form:  
    %                          $ x^T * Q * y $
    %                         Q is specified in the 1st extra parameter 
    %
    %        - quaddiff:       Quadratic form of difference:
    %                          $ (x - y)^T * Q * (x - y) $
    %                         Q is specified in the 1st extra parameter 
    %
    %        - cityblk:        City block distance (abssum of difference)
    %                          $ sum_i |x_i - y_i| $
    %
    %        - maxdiff:        Maximum absolute difference  
    %                          $ max_i |x_i - y_i| $
    %
    %        - mindiff:        Minimum absolute difference
    %                          $ min_i |x_i - y_i| $
    %
    %        - minkowski:      Minkowski distance
    %                          $ (\sum_i |x_i - y_i|^p)^(1/p) $
    %                         The order p is specified in the 1st extra parameter
    %
    %        - wsqdist:        Weighted square of Euclidean distance     
    %                          $ \sum_i w_i (x_i - y_i)^2 $
    %                         the weights w is specified in 1st extra parameter 
    %                         as a d x 1 column vector    
    %
    %        - hamming:        Hamming distance with threshold t
    %                          \{
    %                              ht1 = x > t
    %                              ht2 = y > t
    %                              d = sum(ht1 ~= ht2)                  
    %                          \}
    %                         use threshold t as the first extra param.
    %                         (by default, t is set to zero).
    %
    %        - hamming_nrm:    Normalized hamming distance, which equals the
    %                          ratio of the elements that differ.
    %                          \{
    %                              ht1 = x > t
    %                              ht2 = y > t
    %                              d = sum(ht1 ~= ht2) / length(ht1)                
    %                          \}
    %                          use threshold t as the first extra param.
    %                         (by default, t is set to zero).
    %
    %        - intersect:      Histogram Intersection
    %                           $ d = sum min(x, y) / min(sum(x), sum(y))$
    %
    %        - intersectdis:   Histogram intersection distance
    %                           $ d = 1 - sum min(x, y) / min(sum(x), sum(y)) $
    %
    %        - chisq:          Chi-Square Distance
    %                           $ d = sum (x(i) - y(i))^2/(2 * (x(i)+y(i))) $
    %
    %        - kldiv:          Kull-back Leibler divergence
    %                           $ d = sum x(i) log (x(i) / y(i)) $
    %
    %        - jeffrey:        Jeffrey divergence
    %                           $ d = KL(h1, (h1+h2)/2) + KL(h2, (h1+h2)/2) $
    %      \:}
    %
    % [ Remarks ]
    %   - Both X1 and X2 should be a matrix of numeric values, except
    %     for case when metric type is 'hamming' or 'hamming_nrm'. 
    %     For hamming or hamming_nrm metric, the input matrix can be logical.
    %
    % [ Examples ]
    %   - Compute different types of metrics in pairwise manner
    %     \{
    %         % prepare sample matrix
    %         X1 = rand(10, 100);
    %         X2 = rand(10, 150);
    %
    %         % compute the euclidean distances (L2) 
    %         % between the samples in X1 and X2
    %         M = slmetric_pw(X1, X2, 'eucdist');
    %
    %         % compute the eucidean distances between the samples
    %         % in X1 in a pairwise manner
    %         M = slmetric_pw(X1, X1, 'eucdist');
    %
    %         % compute the city block distances (L1)
    %         M = slmetric_pw(X1, X2, 'cityblk'); 
    %
    %         % compute the normalize correlations
    %         M = slmetric_pw(X1, X2, 'nrmcorr');
    %
    %         % compute hamming distances
    %         M = slmetric_pw(X1, X2, 'hamming', 0.5);
    %         M2 = slmetric_pw((X1 > 0.5), (X2 > 0.5), 'hamming');
    %         assert(isequal(M, M2));
    %     \}
    %
    %   - Compute the parameterized metrics
    %     \{
    %         % compute weighted squared distances with user-supplied weights
    %         weights = rand(10, 1);
    %         M = slmetric_pw(X1, X2, 'wsqdist', weights);
    %
    %         % compute quadratic distances (x-y)^T * Q (x-y)
    %         Q = rand(10, 10);
    %         M = slmetric_pw(X1, X2, 'quaddiff', Q);
    %
    %         % compute Minkowski distance of order 3         
    %         M = slmetric_pw(X1, X2, 'minkowski', 3);
    %     \}
    %
    % [ History ]
    %   - Created by Dahua Lin on Dec 06th, 2005
    %   - Modified by Dahua Lin on Apr 21st, 2005
    %       - regularize the error reporting
    %   - Modified by Dahua Lin on Sep 11st, 2005
    %       - completely rewrite the core codes based on new mex computation 
    %         cores, and the runtime efficiency in both time and space is 
    %         significantly increased.
    %   - Modified by Dahua Lin on Jul 02, 2007
    %       - rewrite the core computation based on the bsxfun introduced in
    %         MATLAB R2007a
    %       - rewrite the core-mex for cityblk, maxdiff, mindiff
    %       - introduce new metrics: corrdist, minkowski
    %   - Modified by Dahua Lin on Jul 30, 2007
    %       - Add the metric types for histograms, which are originally
    %         implemented in slhistmetric_pw in sltoolbox v1.
    %   - Modified by Dahua Lin on Aug 16, 2007
    %       - revise some of the help contents
    %
     
     
    %% parse and verify input arguments
    error(nargchk(3, inf, nargin));
    assert(ischar(mtype), 'sltoolbox:slmetric_pw:invalidarg', ...
        'The metric type should be a string.');
     
    if strcmp(mtype, 'hamming') || strcmp(mtype, 'hamming_nrm')
        assert((isnumeric(X1) || islogical(X1)) && ndims(X1) == 2 && ...
               (isnumeric(X2) || islogical(X2)) && ndims(X2) == 2, ...
            'sltoolbox:slmetric_pw:invalidarg', 'X1 and X2 should be numeric or logical matrices.');        
    else
        assert(isnumeric(X1) && ndims(X1) == 2 && isnumeric(X2) && ndims(X2) == 2, ...
            'sltoolbox:slmetric_pw:invalidarg', 'X1 and X2 should be numeric matrices.');
    end
     
    assert(isa(X2, class(X1)), ...
        'sltoolbox:slmetric_pw:invalidarg', 'X1 and X2 should be of the same class.');
     
    if isempty(X1) || isempty(X2)
        M = [];
        return;
    end
     
     
    %% compute
    switch mtype        
        case {'eucdist', 'sqdist'}
            checkdim(X1, X2);        
            M = bsxfun(@plus, sum(X1 .* X1, 1)', (-2) * X1' * X2);        
            M = bsxfun(@plus, sum(X2 .* X2, 1), M);        
            M(M < 0) = 0;                        
            if strcmp(mtype, 'eucdist')
                M = sqrt(M);
            end 
     
        case 'dotprod'
            checkdim(X1, X2);        
            M = X1' * X2;
     
        case {'nrmcorr', 'corrdist', 'angle'}
            checkdim(X1, X2);
            ns1 = sqrt(sum(X1 .* X1, 1));
            ns2 = sqrt(sum(X2 .* X2, 1));
            ns1(ns1 == 0) = 1;  
            ns2(ns2 == 0) = 1;
            M = bsxfun(@times, X1' * X2, 1 ./ ns1');
            M = bsxfun(@times, M, 1 ./ ns2);
            switch mtype
                case 'corrdist'
                    M = 1 - M;
                case 'angle'
                    M = real(acos(M));
            end
     
        case 'quadfrm'
            Q = varargin{1};       
            M = X1' * Q * X2;
     
        case 'quaddiff'
            checkdim(X1, X2);        
            Q = varargin{1};
            M = X1' * (-(Q + Q')) * X2;
            M = bsxfun(@plus, M, sum(X1 .* (Q * X1), 1)');
            M = bsxfun(@plus, M, sum(X2 .* (Q * X2), 1));        
     
        case 'cityblk'
            checkdim(X1, X2);  
            M = pwmetrics_cimp(X1, X2, int32(1));
     
        case 'maxdiff'
            checkdim(X1, X2); 
            M = pwmetrics_cimp(X1, X2, int32(3));
     
        case 'mindiff'
            checkdim(X1, X2);  
            M = pwmetrics_cimp(X1, X2, int32(2));
     
        case 'minkowski'
            checkdim(X1, X2);
            pord = varargin{1};
            if ~isscalar(pord)
                error('sltoolbox:slmetric_pw:invalidparam', ...
                    'the mikowski order should be a scalar');
            end
            pord = cast(pord, class(X1));        
            M = pwmetrics_cimp(X1, X2, int32(4), pord);
     
        case 'wsqdist'
            d = checkdim(X1, X2);
            w = varargin{1};
            if ~isequal(size(w), [d, 1])
                error('sltoolbox:slmetric_pw:invalidparam', ...
                    'the weights should be given as a d x 1 vector.');
            end              
            wX2 = bsxfun(@times, X2, w);
            M = bsxfun(@plus, (-2) * X1' * wX2, sum(wX2 .* X2, 1));
            clear wX2;        
            wX1 = bsxfun(@times, X1, w);
            M = bsxfun(@plus, M, sum(wX1 .* X1, 1)');      
     
        case {'hamming', 'hamming_nrm'}
            checkdim(X1, X2);
            if islogical(X1) && islogical(X2)
                H1 = X1;
                H2 = X2;
            else
                if isempty(varargin)
                    t = 0;
                else
                    t = varargin{1};
                    assert(isnumeric(t) && isscalar(t), ...
                        'sltoolbox:slmetric_pw:invalidparam', 't should be a numeric scalar.');
                end
                H1 = X1 > t;
                H2 = X2 > t;
            end
            M = pwhamming_cimp(H1, H2);
            if strcmp(mtype, 'hamming_nrm')
                M = M / size(H1, 1);
            end
     
        case 'intersect'
            checkdim(X1, X2);
            M = pwmetrics_cimp(X1, X2, int32(5));
     
        case 'intersectdis'
            checkdim(X1, X2);
            M = 1 - pwmetrics_cimp(X1, X2, int32(5));
     
        case 'chisq'
            checkdim(X1, X2);
            M = pwmetrics_cimp(X1, X2, int32(6));
     
        case 'kldiv'
            checkdim(X1, X2);
            M = pwmetrics_cimp(X1, X2, int32(7));
     
        case 'jeffrey'
            checkdim(X1, X2);
            M = pwmetrics_cimp(X1, X2, int32(8));
     
        otherwise
            error('sltoolbox:slmetric_pw:unknowntype', 'Unknown metric type %s', mtype);
     
     
    end
     
    %% Auxiliary function
     
    function d = checkdim(X1, X2)
     
    d = size(X1, 1);
    if d ~= size(X2, 1)
        error('sltoolbox:slmetric_pw:sizmismatch', ...
            'X1 and X2 have different sample dimensions');
    end

    Mais l’hors de l’appel de cette fonction
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    X1 = rand(10, 100);
    X2 = rand(10, 150);
    weights = rand(10, 1);
    M = slmetric_pw(X1, X2, 'wsqdist', weights);

    Il me donne cette erreur que je n’est pas compris pour le corrigé.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    ??? Error using ==> assert
     
    Error in ==> slmetric_pw at 176
    assert(ischar(mtype), 'sltoolbox:slmetric_pw:invalidarg', ...
    Y-a-t-il quelqu’un S-V-P qui peut m’aider ?

  2. #2
    Rédacteur/Modérateur

    Avatar de Jerome Briot
    Homme Profil pro
    Freelance mécatronique - Conseil, conception et formation
    Inscrit en
    Novembre 2006
    Messages
    20 317
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Freelance mécatronique - Conseil, conception et formation

    Informations forums :
    Inscription : Novembre 2006
    Messages : 20 317
    Par défaut
    Quelle est ta version de MATLAB ?

    Que retourne ceci ?

    Et que retourne ceci ?


  3. #3
    Membre éclairé
    Inscrit en
    Septembre 2009
    Messages
    343
    Détails du profil
    Informations forums :
    Inscription : Septembre 2009
    Messages : 343
    Par défaut
    Voila ce que me retourne version
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
     
    version
     
    ans =
     
    7.0.0.19920 (R14)
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
     
    which assert -all
     
    C:\Documents and Settings\Slaima\Bureau\Dossier\assert.m
    Déjà, j’ai le code de la fonction assert.m dans mon dossier de travaille et voila sa code :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
     
    function assert(varargin)
    %assert(testCondition)
    %   Evaluates the test condition in the caller's context.  If the result is
    %   false, an error is thrown indicating that the test failed.
    %   TESTCONDITION should be a string.
    %
    %assert(truthValue)
    %assert(truthValue,errMsg)
    %   If TRUTHVALUE is zero, an error is thrown.  The text of the error
    %   contains ERRMSG if supplied.  This version of ASSERT was made to
    %   promote compatibility with Kevin Murphy's Bayes Net Toolbox (i.e. he
    %   uses this syntax).  TRUTHVALUE must be a logical or a number.
    %
    %EXAMPLES:
    %   assert('1==2')      ==> assertion error generated
    %   assert(1==2)        ==> assertion error generated (BNT syntax)
    %   assert 1==2;        ==> assertion error generated
    %   x=1; assert('x==1') ==> no error
    %   x=1; assert x==1;   ==> no error
    %   assert('{a==3')     ==> error generated by Matlab (bad assert code)
    %
    %by Gerald Dalley
     
    % Make ourselves compatible with Kevin Murphy's Bayes Net toolbox.
    if (ismember(nargin,[1 2]) && ...
        (islogical(varargin{1}) || isnumeric(varargin{1})))
      if nargin<2, msg = ''; else, msg = varargin{2}; end
      if ~varargin{1}
        error('assertion violated: %s', msg);
      end
      return
    end
     
    % Standard string-based usage
    for i=1:nargin
        if (~ischar(varargin{i}))
            error('Type HELP ASSERT for usage');
        end
    end
     
    testCondition = join(' ', varargin);
    if (~evalin('caller', testCondition))
        error(['ASSERT FAILED: ' testCondition]);
    end
     
     
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    function s = join(d,varargin)
    % This JOIN function is an inlined version of Gerald Dalley's one posted at the
    % Matlab Central website.  It is placed here as a convenience to users that
    % have not downloaded it.
     
    if (isempty(varargin)), 
        s = '';
    else
        if (iscell(varargin{1}))
            s = join(d, varargin{1}{:});
        else
            s = varargin{1};
        end
     
        for ss = 2:length(varargin)
            s = [s d join(d, varargin{ss})];
        end
    end
     
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    function test
    % Copy-and-paste the below code into the command window to test this
    % function. 
     
    clear all;
     
    worked = 0; try, assert('1==2'); catch, worked = 1; end
    if (~worked), error('Test failed'); end
     
    worked = 1; try, x=1; assert('x==1'); catch, worked = 0; end
    if (~worked), error('Test failed'); end
     
    worked = 0; try, assert(1==2); catch, worked = 1; end
    if (~worked), error('Test failed'); end
     
    worked = 0; try, assert('{a==3'); catch, worked = 1; end
    if (~worked), error('Test failed'); end

  4. #4
    Rédacteur/Modérateur

    Avatar de Jerome Briot
    Homme Profil pro
    Freelance mécatronique - Conseil, conception et formation
    Inscrit en
    Novembre 2006
    Messages
    20 317
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Freelance mécatronique - Conseil, conception et formation

    Informations forums :
    Inscription : Novembre 2006
    Messages : 20 317
    Par défaut
    OK tout s'explique... la fonction ASSERT que tu utilises n'est pas celle de MATLAB (elle n'est d'ailleurs pas disponible dans ta version).

    Le code ne fonctionne pas avec la fonction ASSERT que tu as récupérée je ne sais où...

    En même temps, ASSERT sert juste à gérer les erreurs.

    Tu peux par exemple remplacer :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    assert(ischar(mtype), 'sltoolbox:slmetric_pw:invalidarg', ...
        'The metric type should be a string.');
    par

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    if ~ischar(mtype)
       error('The metric type should be a string.');
    end
    Et faire de même avec tous les autres appels à ASSERT

  5. #5
    Rédacteur/Modérateur

    Avatar de Jerome Briot
    Homme Profil pro
    Freelance mécatronique - Conseil, conception et formation
    Inscrit en
    Novembre 2006
    Messages
    20 317
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Freelance mécatronique - Conseil, conception et formation

    Informations forums :
    Inscription : Novembre 2006
    Messages : 20 317
    Par défaut
    Ou encore plus simple, il suffit de supprimer le second argument de tous les appels à ASSERT dans ton code :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    assert(ischar(mtype), 'sltoolbox:slmetric_pw:invalidarg', ...
        'The metric type should be a string.');
    devient simplement :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    assert(ischar(mtype), 'The metric type should be a string.');

  6. #6
    Membre éclairé
    Inscrit en
    Septembre 2009
    Messages
    343
    Détails du profil
    Informations forums :
    Inscription : Septembre 2009
    Messages : 343
    Par défaut
    J’ai supprimé la fonction assert.m de mon dossier de travaille maintenant puisqu’elle n'est pas celle de MATLAB.
    Et j’ai corrigé
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    assert(ischar(mtype), 'sltoolbox:slmetric_pw:invalidarg', ...
        'The metric type should be a string.');
    Avec ceci

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    if ~ischar(mtype)
       error('The metric type should be a string.');
    end



    et voila mon code maitenant
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
     
    function M = slmetric_pw(X1, X2, mtype, varargin)
    %SLMETRIC_PW Compute the metric between column vectors pairwisely
    %
    % [ Syntax ]
    %   - M = slmetric_pw(X1, X2, mtype);
    %   - M = slmetric_pw(X1, X2, mtype, ...);
    %
    % [ Arguments ]
    %   - X1, X2:       the sample matrices
    %   - mtype:        the string indicating the type of metric
    %   - M:            the resulting metric matrix
    %
    % [ Description ]
    %    - M = slmetric_pw(X1, X2, mtype) Computes the metrics between
    %      column vectors of X1 and X2 pairwisely, using the metric
    %      specified by mtype. 
    %
    %      Both X1 and X2 are matrices with each column representing a 
    %      sample. X1 and X2 should have the same number of rows. Suppose
    %      the size of X1 is d x n1, and the size of X2 is d x n2. Then 
    %      the output metric matrix M will be of size n1 x n2, in which
    %      M(i, j) is the metric value between X1(:,i) and X2(:,j).
    %
    %    - M = slmetric_pw(X1, X2, mtype, ...) Some metric types requires
    %      extra parameters, which should be specified in params.
    %
    %      The supported metrics of this function are listed as follows: 
    %      \{:
    %        - eucdist:        Euclidean distance: 
    %                          $ ||x - y|| $
    %
    %        - sqdist:         Square of Euclidean distance: 
    %                          $ ||x - y||^2 $
    %
    %        - dotprod:        Canonical dot product: 
    %                          $ <x,y> = x^T * y $ 
    %
    %        - nrmcorr:        Normalized correlation (cosine angle):
    %                          $ (x^T * y ) / (||x|| * ||y||) $
    %
    %        - corrdist:       Normalized Correlation distance
    %                          $ 1 - nrmcorr(x, y) $
    %
    %        - angle:          Angle between two vectors (in radian)  
    %                          $ arccos (nrmcorr(x, y)) $
    %        - quadfrm:        Quadratic form:  
    %                          $ x^T * Q * y $
    %                         Q is specified in the 1st extra parameter 
    %
    %        - quaddiff:       Quadratic form of difference:
    %                          $ (x - y)^T * Q * (x - y) $
    %                         Q is specified in the 1st extra parameter 
    %
    %        - cityblk:        City block distance (abssum of difference)
    %                          $ sum_i |x_i - y_i| $
    %
    %        - maxdiff:        Maximum absolute difference  
    %                          $ max_i |x_i - y_i| $
    %
    %        - mindiff:        Minimum absolute difference
    %                          $ min_i |x_i - y_i| $
    %
    %        - minkowski:      Minkowski distance
    %                          $ (\sum_i |x_i - y_i|^p)^(1/p) $
    %                         The order p is specified in the 1st extra parameter
    %
    %        - wsqdist:        Weighted square of Euclidean distance     
    %                          $ \sum_i w_i (x_i - y_i)^2 $
    %                         the weights w is specified in 1st extra parameter 
    %                         as a d x 1 column vector    
    %
    %        - hamming:        Hamming distance with threshold t
    %                          \{
    %                              ht1 = x > t
    %                              ht2 = y > t
    %                              d = sum(ht1 ~= ht2)                  
    %                          \}
    %                         use threshold t as the first extra param.
    %                         (by default, t is set to zero).
    %
    %        - hamming_nrm:    Normalized hamming distance, which equals the
    %                          ratio of the elements that differ.
    %                          \{
    %                              ht1 = x > t
    %                              ht2 = y > t
    %                              d = sum(ht1 ~= ht2) / length(ht1)                
    %                          \}
    %                          use threshold t as the first extra param.
    %                         (by default, t is set to zero).
    %
    %        - intersect:      Histogram Intersection
    %                           $ d = sum min(x, y) / min(sum(x), sum(y))$
    %
    %        - intersectdis:   Histogram intersection distance
    %                           $ d = 1 - sum min(x, y) / min(sum(x), sum(y)) $
    %
    %        - chisq:          Chi-Square Distance
    %                           $ d = sum (x(i) - y(i))^2/(2 * (x(i)+y(i))) $
    %
    %        - kldiv:          Kull-back Leibler divergence
    %                           $ d = sum x(i) log (x(i) / y(i)) $
    %
    %        - jeffrey:        Jeffrey divergence
    %                           $ d = KL(h1, (h1+h2)/2) + KL(h2, (h1+h2)/2) $
    %      \:}
    %
    % [ Remarks ]
    %   - Both X1 and X2 should be a matrix of numeric values, except
    %     for case when metric type is 'hamming' or 'hamming_nrm'. 
    %     For hamming or hamming_nrm metric, the input matrix can be logical.
    %
    % [ Examples ]
    %   - Compute different types of metrics in pairwise manner
    %     \{
    %         % prepare sample matrix
    %         X1 = rand(10, 100);
    %         X2 = rand(10, 150);
    %
    %         % compute the euclidean distances (L2) 
    %         % between the samples in X1 and X2
    %         M = slmetric_pw(X1, X2, 'eucdist');
    %
    %         % compute the eucidean distances between the samples
    %         % in X1 in a pairwise manner
    %         M = slmetric_pw(X1, X1, 'eucdist');
    %
    %         % compute the city block distances (L1)
    %         M = slmetric_pw(X1, X2, 'cityblk'); 
    %
    %         % compute the normalize correlations
    %         M = slmetric_pw(X1, X2, 'nrmcorr');
    %
    %         % compute hamming distances
    %         M = slmetric_pw(X1, X2, 'hamming', 0.5);
    %         M2 = slmetric_pw((X1 > 0.5), (X2 > 0.5), 'hamming');
    %         assert(isequal(M, M2));
    %     \}
    %
    %   - Compute the parameterized metrics
    %     \{
    %         % compute weighted squared distances with user-supplied weights
    %         weights = rand(10, 1);
    %         M = slmetric_pw(X1, X2, 'wsqdist', weights);
    %
    %         % compute quadratic distances (x-y)^T * Q (x-y)
    %         Q = rand(10, 10);
    %         M = slmetric_pw(X1, X2, 'quaddiff', Q);
    %
    %         % compute Minkowski distance of order 3         
    %         M = slmetric_pw(X1, X2, 'minkowski', 3);
    %     \}
    %
    % [ History ]
    %   - Created by Dahua Lin on Dec 06th, 2005
    %   - Modified by Dahua Lin on Apr 21st, 2005
    %       - regularize the error reporting
    %   - Modified by Dahua Lin on Sep 11st, 2005
    %       - completely rewrite the core codes based on new mex computation 
    %         cores, and the runtime efficiency in both time and space is 
    %         significantly increased.
    %   - Modified by Dahua Lin on Jul 02, 2007
    %       - rewrite the core computation based on the bsxfun introduced in
    %         MATLAB R2007a
    %       - rewrite the core-mex for cityblk, maxdiff, mindiff
    %       - introduce new metrics: corrdist, minkowski
    %   - Modified by Dahua Lin on Jul 30, 2007
    %       - Add the metric types for histograms, which are originally
    %         implemented in slhistmetric_pw in sltoolbox v1.
    %   - Modified by Dahua Lin on Aug 16, 2007
    %       - revise some of the help contents
    %
     
     
    %% parse and verify input arguments
    error(nargchk(3, inf, nargin));
    if ~ischar(mtype)
       error('The metric type should be a string.');
    end
     
    if strcmp(mtype, 'hamming') || strcmp(mtype, 'hamming_nrm')
        assert((isnumeric(X1) || islogical(X1)) && ndims(X1) == 2 && ...
               (isnumeric(X2) || islogical(X2)) && ndims(X2) == 2, ...
            'sltoolbox:slmetric_pw:invalidarg', 'X1 and X2 should be numeric or logical matrices.');        
    else
        assert(isnumeric(X1) && ndims(X1) == 2 && isnumeric(X2) && ndims(X2) == 2, 'X1 and X2 should be numeric matrices.');
    end
     
    assert(isa(X2, class(X1)), 'X1 and X2 should be of the same class.');
     
    if isempty(X1) || isempty(X2)
        M = [];
        return;
    end
     
     
    %% compute
    switch mtype        
        case {'eucdist', 'sqdist'}
            checkdim(X1, X2);        
            M = bsxfun(@plus, sum(X1 .* X1, 1)', (-2) * X1' * X2);        
            M = bsxfun(@plus, sum(X2 .* X2, 1), M);        
            M(M < 0) = 0;                        
            if strcmp(mtype, 'eucdist')
                M = sqrt(M);
            end 
     
        case 'dotprod'
            checkdim(X1, X2);        
            M = X1' * X2;
     
        case {'nrmcorr', 'corrdist', 'angle'}
            checkdim(X1, X2);
            ns1 = sqrt(sum(X1 .* X1, 1));
            ns2 = sqrt(sum(X2 .* X2, 1));
            ns1(ns1 == 0) = 1;  
            ns2(ns2 == 0) = 1;
            M = bsxfun(@times, X1' * X2, 1 ./ ns1');
            M = bsxfun(@times, M, 1 ./ ns2);
            switch mtype
                case 'corrdist'
                    M = 1 - M;
                case 'angle'
                    M = real(acos(M));
            end
     
        case 'quadfrm'
            Q = varargin{1};       
            M = X1' * Q * X2;
     
        case 'quaddiff'
            checkdim(X1, X2);        
            Q = varargin{1};
            M = X1' * (-(Q + Q')) * X2;
            M = bsxfun(@plus, M, sum(X1 .* (Q * X1), 1)');
            M = bsxfun(@plus, M, sum(X2 .* (Q * X2), 1));        
     
        case 'cityblk'
            checkdim(X1, X2);  
            M = pwmetrics_cimp(X1, X2, int32(1));
     
        case 'maxdiff'
            checkdim(X1, X2); 
            M = pwmetrics_cimp(X1, X2, int32(3));
     
        case 'mindiff'
            checkdim(X1, X2);  
            M = pwmetrics_cimp(X1, X2, int32(2));
     
        case 'minkowski'
            checkdim(X1, X2);
            pord = varargin{1};
            if ~isscalar(pord)
                error('sltoolbox:slmetric_pw:invalidparam', ...
                    'the mikowski order should be a scalar');
            end
            pord = cast(pord, class(X1));        
            M = pwmetrics_cimp(X1, X2, int32(4), pord);
     
        case 'wsqdist'
            d = checkdim(X1, X2);
            w = varargin{1};
            if ~isequal(size(w), [d, 1])
                error('sltoolbox:slmetric_pw:invalidparam', ...
                    'the weights should be given as a d x 1 vector.');
            end              
            wX2 = bsxfun(@times, X2, w);
            M = bsxfun(@plus, (-2) * X1' * wX2, sum(wX2 .* X2, 1));
            clear wX2;        
            wX1 = bsxfun(@times, X1, w);
            M = bsxfun(@plus, M, sum(wX1 .* X1, 1)');      
     
        case {'hamming', 'hamming_nrm'}
            checkdim(X1, X2);
            if islogical(X1) && islogical(X2)
                H1 = X1;
                H2 = X2;
            else
                if isempty(varargin)
                    t = 0;
                else
                    t = varargin{1};
                    assert(isnumeric(t) && isscalar(t),'t should be a numeric scalar.');
                end
                H1 = X1 > t;
                H2 = X2 > t;
            end
            M = pwhamming_cimp(H1, H2);
            if strcmp(mtype, 'hamming_nrm')
                M = M / size(H1, 1);
            end
     
        case 'intersect'
            checkdim(X1, X2);
            M = pwmetrics_cimp(X1, X2, int32(5));
     
        case 'intersectdis'
            checkdim(X1, X2);
            M = 1 - pwmetrics_cimp(X1, X2, int32(5));
     
        case 'chisq'
            checkdim(X1, X2);
            M = pwmetrics_cimp(X1, X2, int32(6));
     
        case 'kldiv'
            checkdim(X1, X2);
            M = pwmetrics_cimp(X1, X2, int32(7));
     
        case 'jeffrey'
            checkdim(X1, X2);
            M = pwmetrics_cimp(X1, X2, int32(8));
     
        otherwise
            error('sltoolbox:slmetric_pw:unknowntype', 'Unknown metric type %s', mtype);
     
     
    end
     
    %% Auxiliary function
     
    function d = checkdim(X1, X2)
     
    d = size(X1, 1);
    if d ~= size(X2, 1)
        error('sltoolbox:slmetric_pw:sizmismatch', ...
            'X1 and X2 have different sample dimensions');
    end

    Encore il me donne une autre erreur l’hors de la même l’appel
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
     
    ??? Undefined command/function 'bsxfun'.
     
    Error in ==> slmetric_pw at 266
            wX2 = bsxfun(@times, X2, w);

    Y-a-t-il quelqu’un S-V-P qui peut m’aider ?

  7. #7
    Membre éclairé
    Inscrit en
    Septembre 2009
    Messages
    343
    Détails du profil
    Informations forums :
    Inscription : Septembre 2009
    Messages : 343
    Par défaut
    A titre d’indication le code la fonction slmetric_pw.m que je l'utilise a été pris à partir de ce lien


    http://www.mathworks.com/matlabcentr...es-and-metrics

  8. #8
    Rédacteur/Modérateur

    Avatar de Jerome Briot
    Homme Profil pro
    Freelance mécatronique - Conseil, conception et formation
    Inscrit en
    Novembre 2006
    Messages
    20 317
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Freelance mécatronique - Conseil, conception et formation

    Informations forums :
    Inscription : Novembre 2006
    Messages : 20 317
    Par défaut
    Citation Envoyé par slaima15 Voir le message
    J’ai supprimé la fonction assert.m de mon dossier de travaille maintenant puisqu’elle n'est pas celle de MATLAB.
    Et j’ai corrigé
    Il fallait modifier toutes les lignes contenant la fonction ASSERT

    Peu importe, relis bien mon dernier message, il y a une solution plus simple.
    Mais il faut que tu récupère à nouveau la fonction ASSERT que tu viens de supprimer

    Bon sinon pour BSXFUN, ça va être un peu plus compliqué...

  9. #9
    Rédacteur/Modérateur

    Avatar de Jerome Briot
    Homme Profil pro
    Freelance mécatronique - Conseil, conception et formation
    Inscrit en
    Novembre 2006
    Messages
    20 317
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Freelance mécatronique - Conseil, conception et formation

    Informations forums :
    Inscription : Novembre 2006
    Messages : 20 317
    Par défaut
    Citation Envoyé par Dut Voir le message
    Bon sinon pour BSXFUN, ça va être un peu plus compliqué...
    On va aller au plus simple...

    Tu remplaces le bloc suivant :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
        case 'wsqdist'
            d = checkdim(X1, X2);
            w = varargin{1};
            if ~isequal(size(w), [d, 1])
                error('sltoolbox:slmetric_pw:invalidparam', ...
                    'the weights should be given as a d x 1 vector.');
            end              
            wX2 = bsxfun(@times, X2, w);
            M = bsxfun(@plus, (-2) * X1' * wX2, sum(wX2 .* X2, 1));
            clear wX2;        
            wX1 = bsxfun(@times, X1, w);
            M = bsxfun(@plus, M, sum(wX1 .* X1, 1)');
    par

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
        case 'wsqdist'
            d = checkdim(X1, X2);
            w = varargin{1};
            if ~isequal(size(w), [d, 1])
                error('sltoolbox:slmetric_pw:invalidparam', ...
                    'the weights should be given as a d x 1 vector.');
            end              
     
            wX2 = X2.*repmat(w,1,size(X2,2)); 
            temp = (-2) * X1' * wX2;
            temp2 = sum(wX2 .* X2, 1);
            M = temp + repmat(temp2,size(temp,1),1); 
            wX1 = X1.*repmat(w,1,size(X1,2));
            temp = sum(wX1 .* X1, 1)';
            M = M + repmat(temp,1,size(M,2));
    Normalement c'est bon...

  10. #10
    Membre éclairé
    Inscrit en
    Septembre 2009
    Messages
    343
    Détails du profil
    Informations forums :
    Inscription : Septembre 2009
    Messages : 343
    Par défaut
    Mais si je veux tester les autres métriques comme 'intersect' ,'chisq', 'maxdiff'
    Comme ceci :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    X1 = rand(10, 100);
    X2 = rand(10, 150);
    M = slmetric_pw(X1, X2,'intersect')
    Il me donne cette erreur
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    ??? Undefined command/function 'pwmetrics_cimp'.
    Pourtant j’ai récupérer tout le continue du dossier « pwmetric » dans mon dossier de travail.
    Et comme j’ai dis tu peux consulter ce dossier avec ce lien pour voir son contenu
    http://www.mathworks.com/matlabcentr...es-and-metrics pour que tu puisses connaitre mon problème.

  11. #11
    Rédacteur/Modérateur

    Avatar de Jerome Briot
    Homme Profil pro
    Freelance mécatronique - Conseil, conception et formation
    Inscrit en
    Novembre 2006
    Messages
    20 317
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Freelance mécatronique - Conseil, conception et formation

    Informations forums :
    Inscription : Novembre 2006
    Messages : 20 317
    Par défaut
    Citation Envoyé par slaima15 Voir le message
    Il me donne cette erreur
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    ??? Undefined command/function 'pwmetrics_cimp'.
    Je pense que ta version de MATLAB ne peut pas utiliser les deux fichiers MEX avec l'extension .mexw32

    Supprimes ces fichiers (ou renomme les) et copie les fichiers .dll que je te donne dans l'archive zip attachée dans le même répertoire.

    Cela fonctionne-t-il ?
    Fichiers attachés Fichiers attachés

  12. #12
    Membre chevronné

    Inscrit en
    Août 2007
    Messages
    302
    Détails du profil
    Informations forums :
    Inscription : Août 2007
    Messages : 302
    Par défaut
    tu pourrais aussi mettre a jour ta version de maltab?

  13. #13
    Rédacteur/Modérateur

    Avatar de Jerome Briot
    Homme Profil pro
    Freelance mécatronique - Conseil, conception et formation
    Inscrit en
    Novembre 2006
    Messages
    20 317
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Freelance mécatronique - Conseil, conception et formation

    Informations forums :
    Inscription : Novembre 2006
    Messages : 20 317
    Par défaut
    Citation Envoyé par paradize3 Voir le message
    tu pourrais aussi mettre a jour ta version de maltab?
    C'est pas faux

  14. #14
    Membre éclairé
    Inscrit en
    Septembre 2009
    Messages
    343
    Détails du profil
    Informations forums :
    Inscription : Septembre 2009
    Messages : 343
    Par défaut
    Oui ça marche avec le .zip que vous m'avez donner, mais possèdes –vous une idée sur un lien qui me donne un code source matlab pour l’utilisation des métriques comme la distance de Hamming, Weighted Chi-Square Distance, Chi-Square Distance, Histogram Intersection Distance… avec un contenu tous écrit avec matlab.J’ai utilisé Google mais j’ai rien trouvé, pouvez –vous m’aider ??

Discussions similaires

  1. Message d'erreur "Error using ==> set"
    Par kira9744 dans le forum Interfaces Graphiques
    Réponses: 22
    Dernier message: 09/10/2009, 11h56
  2. Réponses: 6
    Dernier message: 11/06/2009, 11h24
  3. Fatal error: Using $this when not in object context
    Par lavande dans le forum Langage
    Réponses: 2
    Dernier message: 02/10/2008, 20h17
  4. Error using ==> |
    Par yasminsila dans le forum Images
    Réponses: 3
    Dernier message: 30/05/2008, 08h39
  5. Error using ''fft'' (out of memory ?)
    Par salimilas dans le forum MATLAB
    Réponses: 1
    Dernier message: 21/05/2007, 15h35

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo