Creation of double columns whose max size is 2 x 2 from a matrix whose size is n>2 x n>2 ( e.g. with n max = 800)
    3 visualizzazioni (ultimi 30 giorni)
  
       Mostra commenti meno recenti
    
    Gobert
      
 il 19 Dic 2017
  
    
    
    
    
    Commentato: Gobert
      
 il 20 Dic 2017
            I need your help. Here is an example of what I want to do:
   B = [1 2 3; 4 5 6; 7 8 9]
   [m,n] = size(B);
   B(m-2:m-1, n-2:n-1)
   B(m-1:m  , n-2:n-1)
   B(m-2:m-1, n-1:n)
   B(m-1:m  , n-1:n)
Input:
B =
     1     2     3
     4     5     6
     7     8     9
Output:
ans =
     1     2
     4     5
ans =
     4     5
     7     8
ans =
     2     3
     5     6
ans =
     5     6
     8     9
Can you please show me how I can do this or give me an example to do it automatically, for example for a larger matrix such as B = magic(10) or more?
Thank you!
2 Commenti
  James Tursa
      
      
 il 19 Dic 2017
				These can easily be created, but what do you intend to do with these sub-matrices downstream in your code? There may be a way to do this downstream processing without physically creating all of these sub-matrices.
Risposta accettata
  Harish Ramachandran
    
 il 20 Dic 2017
        
      Modificato: Harish Ramachandran
    
 il 20 Dic 2017
  
      B = rand(2,4);
count = 1;
[xdim ydim] = size(B);
for i=1:xdim-1
for j=1:ydim-1
C{count} = B(i:i+1,j:j+1);
count = count+1;
end
end
In this case, the resultant output will be a cell array with each cell block (C{1}, C{2}...) a distinct 2x2 sub component of the input matrix.
4 Commenti
  Harish Ramachandran
    
 il 20 Dic 2017
				Also, I have edited the code above to make sure it works for non square matrix.
Più risposte (0)
Vedere anche
Categorie
				Scopri di più su Multicore Processor Targets in Help Center e File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


