Is it possible to do this without for cycle?

T = zeros(M,K);
for k = 1:K
T(Y==k,k) = 1;
end

1 件のコメント

Birdman
Birdman 2018 年 3 月 19 日
Share your entire code.

サインインしてコメントする。

 採用された回答

Jos (10584)
Jos (10584) 2018 年 3 月 19 日

0 投票

% data
Y = [1 1 3 2 3 1 1 2] % M = numel(Y)
K = 3
% engine
T = double((Y(:) - (1:K))==0)

2 件のコメント

Mr M.
Mr M. 2018 年 3 月 20 日
beautiful, thanks
Walter Roberson
Walter Roberson 2018 年 3 月 20 日
The above code requires R2016b or later. Earlier versions would need to use bsxfun()

サインインしてコメントする。

カテゴリ

タグ

質問済み:

2018 年 3 月 19 日

コメント済み:

2018 年 3 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by