Can I use unary coding to convert a vector of decimals into a binary matrix?

11 views (last 30 days)
I am trying to convert a decimal vector into a matrix of binary numbers using unary coding. For example:
1 -> 1 0 0 0
2 -> 0 1 0 0
3 -> 0 0 1 0
4 -> 0 0 0 1
I'd like to do this using vector arithmetic, not looping over all of the rows.
  1 Comment
Walter Roberson
Walter Roberson on 18 Mar 2013
There are multiple competing stands for unary coding. Which one are you following? How are you representing 0? How are you representing negative numbers? Do you need to represent fractions?

Sign in to comment.

Accepted Answer

Jan
Jan on 19 Mar 2013
d = 1:4;
n = length(d);
m = max(d);
u = [];
u(sub2ind([n, m], 1:n, d)) = 1;

More Answers (0)

Community Treasure Hunt

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

Start Hunting!