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

조회 수: 2 (최근 30일)
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
Stephen23
Stephen23 2018년 1월 30일
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...
Stephen23
Stephen23 2018년 1월 30일
편집: Stephen23 2018년 1월 30일
@CodeElinesa: it helps us if you post comments as comments, not answers.

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2018년 1월 30일
hint 1:
diag(b(1:end-1), -1)
hint 2: you can add matrices together.
  댓글 수: 5
CodeElinesa
CodeElinesa 2018년 1월 30일
wait, its ok, I found the way. Thank you XD

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Andrei Bobrov
Andrei Bobrov 2018년 1월 30일
편집: Andrei Bobrov 2018년 1월 30일
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)');

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by