[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Slicing submatrices -- more graceful/ vectorized way?
From: |
Jaroslav Hajek |
Subject: |
Re: Slicing submatrices -- more graceful/ vectorized way? |
Date: |
Fri, 20 Aug 2010 21:12:50 +0200 |
On Fri, Aug 20, 2010 at 7:03 PM, forkandwait <address@hidden> wrote:
> 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
> the
> 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
> county.)
>
>
> The code is something like this now:
>
> x={}
> cntyagesex = reshape(data(:,end), 39*36, []);
> for ii = 1:39
> starti = (ii-1)*36 + 1;
> endi = ii * 36;
> x(ii) = cntyagesex(starti:endi,:);
> endfor
>
> I am hoping for a one-liner with kron() and reshape(), .... ;)
>
Hehe, yes, but I think the reshape would be 3D...
Face it: if you have a set of equal size, equal type matrices, 3D
array *is* the best solution. Still you can simplify your code, for
instance using the fact that ranges are expressions too:
cntyagesex = reshape(data(:,end), 39*36, []);
idx = 1:36;
for ii = 1:39
idx += 36;
x{ii} = cntyagesex(starti:endi,:);
endfor
--
RNDr. Jaroslav Hajek, PhD
computing expert & GNU Octave developer
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz