Creating a matrix which has particular order of element ?

1 vue (au cours des 30 derniers jours)
P K
P K le 21 Jan 2019
Modifié(e) : Andrei Bobrov le 21 Jan 2019
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 !

Réponse acceptée

Andrei Bobrov
Andrei Bobrov le 21 Jan 2019
Modifié(e) : Andrei Bobrov le 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';

Plus de réponses (1)

Kevin Phung
Kevin Phung le 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 .

Catégories

En savoir plus sur Mathematics and Optimization dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by