Is it possible to do this without for cycle?
1 view (last 30 days)
Show older comments
T = zeros(M,K);
for k = 1:K
T(Y==k,k) = 1;
end
Accepted Answer
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
on 20 Mar 2018
The above code requires R2016b or later. Earlier versions would need to use bsxfun()
More Answers (1)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!