Is it possible to do this without for cycle?

1 view (last 30 days)
T = zeros(M,K);
for k = 1:K
T(Y==k,k) = 1;
end

Accepted Answer

Jos (10584)
Jos (10584) on 19 Mar 2018
% data
Y = [1 1 3 2 3 1 1 2] % M = numel(Y)
K = 3
% engine
T = double((Y(:) - (1:K))==0)
  2 Comments
Walter Roberson
Walter Roberson on 20 Mar 2018
The above code requires R2016b or later. Earlier versions would need to use bsxfun()

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 19 Mar 2018

Tags

Community Treasure Hunt

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

Start Hunting!