Take a matrix of integers and convert to a binary matrix

2 vues (au cours des 30 derniers jours)
Emma Humphrey
Emma Humphrey le 4 Sep 2018
I have a matrix of random integers, but the rows are sorted numerically from 1-10. I would like to convert these integers into positions of a binary matrix, so each integer in the matrix represents a position labelled 1 in the binary matrix.
e.g As a smaller scale example, (cause my final integer matrix is very large) Say I have a matrix given by
1 6 8
3 5 7
2 4 9
I would want this converted to a 10 x 3 matrix that reads
1 0 0 0 0 1 0 1 0 0
0 0 1 0 1 0 1 0 0 0
0 2 0 1 0 0 0 0 1 0
Many thanks

Réponses (1)

Akira Agata
Akira Agata le 4 Sep 2018
One simple and straight-forward way is using for-loop, like:
A = [1 6 8; 3 5 7; 2 4 9];
B = zeros(3,10);
for kk = 1:3
B(kk,A(kk,:)) = 1;
end
The result is:
>> B
B =
1 0 0 0 0 1 0 1 0 0
0 0 1 0 1 0 1 0 0 0
0 1 0 1 0 0 0 0 1 0

Catégories

En savoir plus sur Numeric Types dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by