MATLAB Answers

0

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

Asked by Vincent Gambuzza on 15 Mar 2019
Latest activity Edited by Andrei Bobrov
on 15 Mar 2019
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 Comments

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

Sign in to comment.

2 Answers

Answer by Andrei Bobrov
on 15 Mar 2019
Edited by Andrei Bobrov
on 15 Mar 2019
 Accepted Answer

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;

  0 Comments

Sign in to comment.


Answer by Jos (10584)
on 15 Mar 2019

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

  1 Comment

Impacts Andrei's second solution on a larger scale ;-)

Sign in to comment.