How to change a matrix as inserting zeroes both end sides?

I wanna make 'Az2' matrix 'Az' in the picture.
This is my code for doing that,
for o=2:1:zmax-1;
Az(o,o-1:o+1)=Az2(o,1:3);
end
however, it's way slow... Can I have any other better coding for that as using vectorization? help me...

 채택된 답변

Sean de Wolski
Sean de Wolski 2013년 11월 18일
편집: Sean de Wolski 2013년 11월 18일
There's a gallery for that!
az = rand(10,3);
Az2 = full(gallery('tridiag',az(2:end,1),az(:,2),az(1:end-1,3)))
And
doc gallery

댓글 수: 2

Unbeilvable! Thanks alot. you are my life saver!
Hi!
But in this case Az2 is a square matrix. As I understood this was not desired?!?

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

추가 답변 (1개)

Simon
Simon 2013년 11월 18일

0 개 추천

댓글 수: 3

Thanks for answer, but... that matrix is not square.
Hi!
It doesn't matter!
% diagonal elements
Az2 = rand(10, 3);
% diagonal 1
A1 = diag(Az2(:, 1));
A1 = [A1, zeros(size(A1, 1), 2)];
% diagonal 2
A2 = diag(Az2(:, 2), 1);
A2(end, :) = [];
A2 = [A2, zeros(size(A2, 1), 1)];
% diagonal 3
A3 = diag(Az2(:, 2), 2);
A3(end-1:end, :) = [];
% combine matrices
Az = A1 + A2 + A3;
% row with zeros in front
Az = [zeros(1, size(Az, 2)); Az];
amazing! Thanks alot. you are my life saver!

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

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2013년 11월 18일

댓글:

2013년 11월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by