Creating a matrix which has particular order of element ?

1 次查看(过去 30 天)
Hello all,
I have to obtain a matrix. The matrix looks like
A=[1 0 0 0 0 0;0 2 0 0 0 0;3 0 4 0 0 0;0 5 0 6 0 0;7 0 8 0 9 0;0 10 0 11 0 12];
% There is a sequence.Odd row and even row.
% ODD ROW- Element stasts from first column
% EVEN ROW- Element start from second coloumn
% Each element is +1 to the previous element.
I can do this with "FOR-LOOP". But is there some effiecient way to do this.
Thanks you !

采纳的回答

Andrei Bobrov
Andrei Bobrov 2019-1-21
编辑:Andrei Bobrov 2019-1-21
m = 6;
n = 6;
lo = triu(~rem((1:n)' + (1:m),2));
out = int64(lo);
out(lo) = 1:nnz(lo);
out = out';
with kron
m = 7;
n = 6;
out = triu(kron(ones(ceil(n/2),ceil(m/2)),[1,0;0,1]));
out = out(1:n,1:m);
out(out~=0) = 1:nnz(out);
out = out';

更多回答(1 个)

Kevin Phung
Kevin Phung 2019-1-21
Can you be more clear in what you are trying to do? Is this pattern supposed to go on? Or is this matrix A that you have provided exactly what you need, and you are looking for a way to programmatically create it rather than typing it out .

类别

Help CenterFile Exchange 中查找有关 Mathematics and Optimization 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by