Effacer les filtres
Effacer les filtres

Creating matrix of given pattern

7 vues (au cours des 30 derniers jours)
Peter Mbamaluikem
Peter Mbamaluikem le 3 Avr 2017
I want to write a matlab code that will create 11x880 matrix. the first 80 columns of row one will have 1's and the rest zeros. the second row will have its own 1's from column 81 to column 160 and the third row will be from 161 to next 80 and so.
Inpu = zeros(11,880);
Inpu(1, (1:80)) = ones(1,1:80) %this will write into the first 80
Inpu(2,(81:160)) = ones(1,1:80) % will write in the second row starting from 81 to 160
What command will be able to iterate it up to the 11th row which will have its 1's between column 801 to 880. Thanks

Réponse acceptée

the cyclist
the cyclist le 3 Avr 2017
Inpu = zeros(11,880);
for ii = 1:11
Inpu(ii,80*(ii-1)+1:ii*80) = ones(1,80);
end
  1 commentaire
Peter Mbamaluikem
Peter Mbamaluikem le 3 Avr 2017
Am very grateful, God bless you.

Connectez-vous pour commenter.

Plus de réponses (2)

Matt J
Matt J le 3 Avr 2017
Modifié(e) : Matt J le 3 Avr 2017
Inpu=kron(eye(11),ones(1,80));

dpb
dpb le 3 Avr 2017
Modifié(e) : dpb le 3 Avr 2017
For a much smaller size array so can see the results; size is actually immaterial to logic--
>> N=2; M=3; % pick sizes
>> A=zeros(M,M*N); % make overall match those as wanted
>> O=ones(1,N); % make the ones vector
>> j=1; % initial column position
for i=1:M % for the number rows
A(i,j:j+N-1)=O; % set the locations desirec
j=j+N; % next first column
end
>> A % see result is what wanted...
A =
1 1 0 0 0 0
0 0 1 1 0 0
0 0 0 0 1 1
>>
Alternatively with a slightly different starting position--
>> A=zeros(M,M*N); % make overall match those as wanted
>> O=ones(1,N); % make the ones vector
>> A(:,1:N)=repmat(O,M,1); % load first N columns every row
>> for i=2:M % move rows after first to right
A(i,:)=circshift(A(i,:),2*(i-1),2);
end
>> A
A =
1 1 0 0 0 0
0 0 1 1 0 0
0 0 0 0 1 1
>>
Sometimes a loop is just by far the simplest way...
  1 commentaire
Peter Mbamaluikem
Peter Mbamaluikem le 3 Avr 2017
Very correct. God bless you

Connectez-vous pour commenter.

Catégories

En savoir plus sur Creating and Concatenating Matrices 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