[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Slicing submatrices -- more graceful/ vectorized way?

From: forkandwait
Subject: Slicing submatrices -- more graceful/ vectorized way?
Date: Fri, 20 Aug 2010 17:03:29 +0000 (UTC)
User-agent: Loom/3.14 (

Hi all,

Currently I have a loop that goes down a matrix and pulls out submatrices of 36
 rows (each submatrix is a county, and there are 39 counties, so there are 1404 
rows total).  

Is there a more graceful way to handle this?  I always mess up my index 
calculations ("starti" and "endi" below) and it pisses me off.  Another 
constraint is that I would like to avoid 3-d matrices, though maybe they are 
best solution (I find that code that uses more than 2-d becomes brittle when 
other people try to maintain it -- hence the cellarray "x" to store each 

The code is something like this now:

cntyagesex = reshape(data(:,end), 39*36, []);
for ii = 1:39
  starti = (ii-1)*36 + 1;
  endi = ii * 36;
  x(ii) = cntyagesex(starti:endi,:);

I am hoping for a one-liner with kron() and reshape(), .... ;)


reply via email to

[Prev in Thread] Current Thread [Next in Thread]