Creating a matrix which has particular order of element ?

1 view (last 30 days)
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 !

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 21 Jan 2019
Edited: Andrei Bobrov on 21 Jan 2019
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';

More Answers (1)

Kevin Phung
Kevin Phung on 21 Jan 2019
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 .

Categories

Find more on Mathematics and Optimization 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!