how to make a column vector with evenly spaced entries with different coefficients

4 次查看(过去 30 天)
I am trying to make the following 16x1 column vector
y = 3 %given, could be any value
ycoeff = [0;0;0;-4y;0;0;0;-3y;0;0;0;-2y;0;0;0-y]
If you could help me i would be very appreciative.
Thank you!
  2 个评论
Jos (10584)
Jos (10584) 2019-3-15
You forgot to add the multiplication symbol, and a semi-colon is missing before the last y ...
y = 3 %given, could be any value
ycoeff = [0;0;0;-4*y;0;0;0;-3*y;0;0;0;-2*y;0;0;0 ; -y]
Yet, somehow I feel that this is not the real question you want to have asnwers to ... :-D

请先登录,再进行评论。

采纳的回答

Andrei Bobrov
Andrei Bobrov 2019-3-15
编辑:Andrei Bobrov 2019-3-15
k = -4:-1;
y = 3;
n = numel(k);
y = [zeros(n-1,n);y*k(:)'];
ycoeff = y(:);
or
k = -4:-1;
y = 3;
n = numel(k);
ycoeff = zeros(n^2,1);
ycoeff(n:n:end) = k*y;

更多回答(1 个)

Jos (10584)
Jos (10584) 2019-3-15
A simple one-liner:
y = 3 ;
k = 4 ;
ycoeff(k:k:(k*k), 1) = (-k:-1)*y

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by