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

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...
Accepted Answer
More Answers (1)
Simon
on 18 Nov 2013
0 votes
3 Comments
HOEMIN
on 18 Nov 2013
Simon
on 18 Nov 2013
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];
HOEMIN
on 19 Nov 2013
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!