filling a matrix with a loop
    6 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
    bus14
 el 14 de Mayo de 2019
  
Hi community, 
I want to create a large matrix 400x400, In this matrix I want it to have the form
A= [1 1 0 1 000000000000000000........;0 1 1 0 1 00000000000000......; 0 0 1 1 0 1 00000000000000000] and so on till it is a 400x400 matrix.
I could not find a way yet to easiliy do this. As doing this manually is way too much work. 
Does anyone know how to do this?
2 comentarios
Respuesta aceptada
  Stephen23
      
      
 el 14 de Mayo de 2019
        
      Editada: Stephen23
      
      
 el 14 de Mayo de 2019
  
      "Does anyone know how to do this?"
>> C = [1,zeros(1,399)];
>> R = [1,1,0,1,zeros(1,396)];
>> M = toeplitz(C,R);
>> size(M)
ans =
   400   400
>> M(1:9,1:20) % first rows and columns
ans =
   1   1   0   1   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
   0   1   1   0   1   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
   0   0   1   1   0   1   0   0   0   0   0   0   0   0   0   0   0   0   0   0
   0   0   0   1   1   0   1   0   0   0   0   0   0   0   0   0   0   0   0   0
   0   0   0   0   1   1   0   1   0   0   0   0   0   0   0   0   0   0   0   0
   0   0   0   0   0   1   1   0   1   0   0   0   0   0   0   0   0   0   0   0
   0   0   0   0   0   0   1   1   0   1   0   0   0   0   0   0   0   0   0   0
   0   0   0   0   0   0   0   1   1   0   1   0   0   0   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   1   1   0   1   0   0   0   0   0   0   0   0
>> M(392:400,381:400) % last rows and columns
ans =
   0   0   0   0   0   0   0   0   0   0   0   1   1   0   1   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   0   0   0   1   1   0   1   0   0   0   0
   0   0   0   0   0   0   0   0   0   0   0   0   0   1   1   0   1   0   0   0
   0   0   0   0   0   0   0   0   0   0   0   0   0   0   1   1   0   1   0   0
   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   1   1   0   1   0
   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   1   1   0   1
   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   1   1   0
   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   1   1
   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   1
4 comentarios
  Stephen23
      
      
 el 16 de Mayo de 2019
				
      Editada: Stephen23
      
      
 el 16 de Mayo de 2019
  
			C = {[1,0;1,1;0,1;1,0]};
M = blkdiag(C{ones(1,200)});
Checking:
>> size(M)
ans =
   800   400
>> M(1:12,1:16)
ans =
   1   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
   1   1   0   0   0   0   0   0   0   0   0   0   0   0   0   0
   0   1   0   0   0   0   0   0   0   0   0   0   0   0   0   0
   1   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
   0   0   1   0   0   0   0   0   0   0   0   0   0   0   0   0
   0   0   1   1   0   0   0   0   0   0   0   0   0   0   0   0
   0   0   0   1   0   0   0   0   0   0   0   0   0   0   0   0
   0   0   1   0   0   0   0   0   0   0   0   0   0   0   0   0
   0   0   0   0   1   0   0   0   0   0   0   0   0   0   0   0
   0   0   0   0   1   1   0   0   0   0   0   0   0   0   0   0
   0   0   0   0   0   1   0   0   0   0   0   0   0   0   0   0
   0   0   0   0   1   0   0   0   0   0   0   0   0   0   0   0
Más respuestas (3)
  Alex Mcaulley
      
 el 14 de Mayo de 2019
        A = repmat([1 1 0 1 zeros(1,396)],400,1);
A = cell2mat(arrayfun(@(i) circshift(A(i,:),i-1) , 1:size(A,1), 'UniformOutput',false)');
0 comentarios
  Jos (10584)
      
      
 el 14 de Mayo de 2019
        % clever indexing trick
A= [1 1 0 1]
N = 10 ; % smaller example! 400 in your case
X = triu(toeplitz(1:N)) ;
X(X > numel(A)) = 0 ;
tf = X > 0 ;
X(tf) = A(X(tf))
0 comentarios
  Andrei Bobrov
      
      
 el 15 de Mayo de 2019
        
      Editada: Andrei Bobrov
      
      
 el 15 de Mayo de 2019
  
      out = full(spdiags(ones(400,3),[0,1,3],400,400));
0 comentarios
Ver también
Categorías
				Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!





