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

10 vues (au cours des 30 derniers jours)
Rashid
Rashid le 18 Mar 2013
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 commentaire
Walter Roberson
Walter Roberson le 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?

Connectez-vous pour commenter.

Réponse acceptée

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

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by