Is it possible to do this without for cycle?

 Accepted Answer

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

2 Comments

beautiful, thanks
The above code requires R2016b or later. Earlier versions would need to use bsxfun()

Sign in to comment.

Categories

Find more on MATLAB in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!