1

I need to do the following:

A = [1,2,3; 0,4,0; 0,5,6]
B = krone (A, A)
C = sparse (dummy (B))

Could anyone tell me how to create a dummy variable in Scilab. I am looking for a command equivalent to the dummy var in Matlab.

4

1 回答 1

1

There is no built-in Scilab version of dummyvar command in Matlab, but here is a function that does the same for numerical data.

function D = dummyvar(B)
    m = size(B, 1)
    D = zeros(m, 0)
    for j = 1:size(B, 2)
        E = zeros(m, max(B(:, j)))
        for i = 1:m
            E(i, B(i, j)) = 1
        end
        D = [D, E] 
    end
endfunction

Example of usage:

B = [1 2 1; 3 1 2; 1 2 2]
dummyvar(B)

Output:

1.    0.    0.    0.    1.    1.    0.  
0.    0.    1.    1.    0.    0.    1.  
1.    0.    0.    0.    1.    0.    1.  

This output is the same as dummyvar in Matlab returns.

Quoting the documentation:

If group is a numeric vector or matrix, values in any column must be positive integers in the range from 1 to the number of levels for the corresponding variable

This is the only kind of data that my function supports; it does not deal with the categorical data. I'll point out that the code sample you gave would throw an error in Matlab too, because the matrix is numerical and contains zeros.

于 2016-03-18T21:43:52.430 回答