I want to create a n by 1 matrix. The FDF2=2 is on the top row, while BDF2=3 is on the bottom. And the values of the middle row should all equal to 3. How do I input the values in such matrix?
1 view (last 30 days)
John D'Errico on 17 Apr 2017
Edited: John D'Errico on 17 Apr 2017
I'm not sure why you would initially create a VECTOR of length n+1, if your final goal is an nx1 vector. :)
No. There is no need to preallocate the vector with zeros, although your mentioning that idea is the only reason I was willing to answer your question. There are lots of ways to create this vector though. A simple one is:
V = [FDF2;repmat(3,n-2,1);BDF2];
An alternative is to preallocate a vector, then stuff the first and last elements.
V = ones(n,1)*3;
V([1 end]) = [FDF2,BDF2];
Lots of other ways to do this, but that covers the first ones I might think of. You can see that the first solution involves concatenation of the indicated elements, whereas the second had me create a vector of the final size, then stuffing the first and last elements as desired. Either is fine.