フィルターのクリア

How to convert integer vector with the 1-of-K coding sheme into a matrix?

1 回表示 (過去 30 日間)
Mr M.
Mr M. 2018 年 1 月 30 日
コメント済み: Andrei Bobrov 2018 年 1 月 30 日
I have Y = [2 2 1 3 2 4 1 4 4 3]; And I need 4 x 10 matrix in which all values are zeros, except 2nd element in the 1st column, 2nd element in the 2nd column, 1st element in the 3rd column etc. they are all ones.

回答 (2 件)

Jos (10584)
Jos (10584) 2018 年 1 月 30 日
Here are two easy one-liners:
Y = [2 2 1 3 2 4 1 4 4 3];
M1 = full(sparse(Y,1:10,1))
M2 = accumarray([Y(:) (1:10).'], 1)

Andrei Bobrov
Andrei Bobrov 2018 年 1 月 30 日
Y = [2 2 1 3 2 4 1 4 4 3];
m = max(Y);
n = numel(Y);
out = zeros(m,n);
out( sub2ind([m,n],Y,1:n )) = 1

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by