For example, given i=5 and and n=8, I want to generate [0;0;0;0;1;0;0;0]. Specifically, I want to generate the vector v so that:
v = zeros(n,1);
v(i) = 1;
Is there a (reasonable) way to do this in one line?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Here’s another solution using
sparseto create annlength row vector with a 1 in theith position:The advantage is that for large
n, this is also memory efficient and can be used as usual in matrix calculations. If you ever need the full vector, just usefull.