kathystehl
kathystehl

Reputation: 841

Create an nx1 vector that can be changed easily by specifying n

I want to be able to create a simple nx1 vector in which each row value is a constant (e.g. [2 2 ... 2 2]') and also an nx1 vector in which the values in row 1 and row n are specified (e.g. [1 2 2 ... 2 2 1]'). Also, how would you generate a vector in which you are alternating between two values (e.g. [1 -1 1 -1...]')?

Is there anyway to generate these vectors with out manually typing in each value? I tried to find a way to do so by looking through this Matlab documentation, but couldn't work it out. Thank you!

Upvotes: 0

Views: 349

Answers (1)

KKS
KKS

Reputation: 1389

const=2; %desired constant value
len=5; %length of vector
row1=1; %value of row 1
rown=1; %value of row n

x=const*ones(len,1);
y=[row1; x(1:end-2); rown];

Please, try this code.

const=5; %desired absolute value
Len=10; %length of vector
k=1:Len;

a=(-1).^k;
b=const*a;

If you want same absolute number, this code would be ok.

const1=-3; %first value
const2=5; %second value
N=5;  %half length of vector 

a=const1*ones(1,N);
b=const2*ones(1,N);

k=zeros(1,2*N);

n=1:N;
k(2*n)=a(n);
k(2*n-1)=b(n);

If you want arbitrary two values, please try this code.

Upvotes: 0

Related Questions