matlab - Construct a matrix from a vector periodically -
this question has answer here:
i have vectorb
30 entries.
i want avoid using for
loop construct matrix this:
where b_i
i
-th entry of vector b
.
for example, define vector
b = [2 6 -7 3 1 -4 -1 1 11 8 -4 9 2 0 2 -1 0 4 4 4 2 -4 2 5 1 3 2 -1 1 -2]
where tried using loop is:
a = zeros(5,5); = 1:5 a(i) = b(i+5); a(i+5) = b(i+6); a(i+10) = b(i+7); a(i+15) = b(i+8); a(i+20) = b(i+9); end
the result
is there faster , more general method generate matrix?
you can use toeplitz
:
a=fliplr(toeplitz(b(10:14),b(10:-1:6)) = -4 -1 1 11 8 -1 1 11 8 -4 1 11 8 -4 9 11 8 -4 9 2 8 -4 9 2 0
by way, indices here 6 14 in example, , not 7 15 in picture. can change preferred purpose.
Comments
Post a Comment