Reputation: 467
I have row and column in matlab:
a = [1 0 3 ... a_k]; - row 1xk
b = [1;0;3; ... b_k]; - column kx1
I want to create new matrix's (A and B) mxn, that can be populate by shift row and column:
A = [1 0 3 0 0 0 0 ... 0;
0 1 0 3 0 0 0 ... 0;
0 0 1 0 3 0 0 ... 0;
...
0 0 0 0 0 0 ... 1 0 3 ]
B= [1 0 0 0 0 0 0 ... 0;
0 1 0 0 0 0 0 ... 0;
3 0 1 0 0 0 0 ... 0;
0 3 0 1 0 0 0 ... 0;
0 0 3 0 1 0 0 ... 0;
...
0 0 0 0 0 0 0 ... 3]
How can I do it?
Upvotes: 0
Views: 889
Reputation: 112659
Is this what you want?
>> a = [1 0 3];
>> m = 5; %// number of rows
>> A = convmtx(a,m)
A =
1 0 3 0 0 0 0
0 1 0 3 0 0 0
0 0 1 0 3 0 0
0 0 0 1 0 3 0
0 0 0 0 1 0 3
>> b = [1;0;3];
>> m = 4; %// number of columns
>> B = convmtx(b,m)
B =
1 0 0 0
0 1 0 0
3 0 1 0
0 3 0 1
0 0 3 0
0 0 0 3
Upvotes: 1
Reputation: 4768
You can do this in a slightly tricky way by using a combination of indexing and bsxfun
. First we want to create an index matrix that represents the shift that we're trying to. It should look like this (at least for A
):
1 2 3 4 ... k
k 1 2 3 ... k-1
etc
To create this, we can use bsxfun
as follows:
index = mod(bsxfun(@plus,1:k,-(1:(k-2))'),k)+1;
We can then create the matrix A
by using this as an index matrix for a
:
A = a(index);
The matrix B
is the same, just transposed:
B = b(index)';
Upvotes: 1