How to create a special diagonal matrix with 2 vectors together?

my problem is those vectors are given,but you have to put them like image shown below I dont know how to change the dimension of B so that it can fit into the matrix A = linspace(1,6,6) B = linspace(11,16,6) P.S the question asked us to use diag...

4 Comments

What have you done so far? What specific problems are you having with your code?
Also, please post your text in the Questions box (or Comments to the Question), and not as Answers as you have done below.
CodeElinesa's "Answer" moved here:
forgot the image, sorry
CodeElinesa's "Answer" moved here:
Also, I want to know the way to use loop. So you could deal with both scalar and vector example for a and b...
@CodeElinesa: it helps us if you post comments as comments, not answers.

Sign in to comment.

 Accepted Answer

hint 1:
diag(b(1:end-1), -1)
hint 2: you can add matrices together.

5 Comments

does that mean we have start-1 as well?(I dont have laptop with me atm)
cuz there is b vector which starts with b2 to bn, how would you present it?
wait, its ok, I found the way. Thank you XD

Sign in to comment.

More Answers (1)

full(gallery('tridiag',B(2:end),A,B(1:end-1)));
or
n = numel(A);
out = full(spdiags([B(:),A(:),B(:)],-1:1,n,n)');

Categories

Community Treasure Hunt

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

Start Hunting!