Skip to content

Instantly share code, notes, and snippets.

@Link-

Link-/vm-rp

Created Sep 28, 2017
Embed
What would you like to do?
Solution to a vector to matrix recoding problem
A is 100 dimensional vector containing integers N between [1 and 10] (inclusive) distributed at intervals of 10:
i.e.
at index x -> N
at index 1 -> 1
at index 11 -> 2
at index 21 -> 3
Problem:
-----------
Need to recode A into a 10x100 (Rows x Columns) matrix such that each column is a 10 dimensional vector y with y(x) = 1 the rest is 0
i.e.
For x between 1 and 10 we will have the same vector (y) replicated 10 times with with y =
111...0
000...1
000...0
000...0
000...0
000...0
000...0
000...0
000...0
000...0
Expected Solution
------------------
I need a vectorized implementation of the above i.e. no for loops and index manipulation, that's easy.
Actual Solution in Matlab/Octave
---------------------------------
x = [1 1 1 2]
A = zeros(10, 4)
A(sub2ind(size(A), x, 1:4)) = 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment