Vectorizing the creation of an (m x n) matrix where each row represents a number
Infos
Cette question est clôturée. Rouvrir pour modifier ou répondre.
Afficher commentaires plus anciens
I am trying to create an (m x n) matrix where every row represents a number. For instance, let's say I wanted to represent the following numbers:
y = [1:3]'
y =
1
2
3
Then that could be represented as:
yk = [1 0 0; 0 1 0; 0 0 1]
yk =
1 0 0
0 1 0
0 0 1
Right now I am using the following code:
yk = zeros(m,n);
for i = 1:m
yk(i,y(i)) = 1;
end
But I would like to vectorize it to get rid of the FOR loop. Anyone know how to do this?
Réponses (3)
the cyclist
le 16 Jan 2017
Modifié(e) : the cyclist
le 16 Jan 2017
I'm not sure how your matrix "represents" the vector. I feel that you have not presented a general enough rule for what you want.
Do you just want
eye(3)
?
1 commentaire
Jeffrey Osborne
le 16 Jan 2017
Roger Stafford
le 16 Jan 2017
Assuming y is a column vector
yk = zeros(m,n);
yk((1:m)’+m*(y-1)) = 1;
1 commentaire
Roger Stafford
le 16 Jan 2017
Perhaps the first line should be:
yk = zeros(m,max(y));
Walter Roberson
le 16 Jan 2017
0 votes
https://www.mathworks.com/help/nnet/ref/ind2vec.html if you have the Neural Network toolbox
Cette question est clôturée.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!