create a matrix with numbers from vector
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Elysi Cochin
le 13 Fév 2019
Commenté : madhan ravi
le 13 Fév 2019
i have a vector as
v = [ 1 1 1 2 2 2 3 3 4]
i wanted to create a new matrix as
M = [
1 1 1 0 0 0 0 0 0;
0 0 0 1 1 1 0 0 0;
0 0 0 0 0 0 1 1 0;
0 0 0 0 0 0 0 0 1];
how to do it?
0 commentaires
Réponse acceptée
madhan ravi
le 13 Fév 2019
v = [1 1 1 2 2 2 3 3 4];
u=unique(v);
R=arrayfun(@(x)v==u(x),1:numel(u),'un',0);
M=+vertcat(R{:})
Gives:
M =
1 1 1 0 0 0 0 0 0
0 0 0 1 1 1 0 0 0
0 0 0 0 0 0 1 1 0
0 0 0 0 0 0 0 0 1
0 commentaires
Plus de réponses (2)
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!