How do I use a vector as a set of indices?
7 views (last 30 days)
So, I have a 5000x1 vector y, the entries of y are all integer values between 1 and 10.
I want to create a new matrix, yrec, that is 10x5000, such that if y(1)=10 then yrec(10,1)=1, if y(2)=8 then yrec(8,2)=1, if y(3)=4 then yrec(4,3)=1 etc.
and all other entries in that row are equal to zero. I can do this through a for loop quite easily as follows
yrec=zeros(num_labels,m); %recoded y
which works without issue. However, I was wondering if there is a vectorized way to do this? hopefully more computationally efficient than a for loop?
but this just sets every entry in yrec equal to 1 for some reason.