Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: divide and conquer matrix
Date: Fri, 19 Jun 2009 09:40:18 +0000 (UTC)
Organization: Erasmus MC
Lines: 47
Message-ID: <h1fme2$3oj$1@fred.mathworks.com>
References: <h1e92a$bd7$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1245404418 3859 172.30.248.37 (19 Jun 2009 09:40:18 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Fri, 19 Jun 2009 09:40:18 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 870065
Xref: news.mathworks.com comp.soft-sys.matlab:548864


"Diego Lass" <dlISCool@gmail.com> wrote in message <h1e92a$bd7$1@fred.mathworks.com>...
> Hi 
> I have a problem with a large matrix, what I want to do is to divide the matrix into 4 submatrices, do some operation then reassemble the submatrix together.  A toy example
> 
> A = [1 2 3 ; 3 4 5 ; 2 3 4; 5 1 2 ]
> A =
> 
>      1     2     3
>      3     4     5
>      2     3     4
>      5     1     2
>  I want to divide A into ANY approximately equal sized partitions, for example
> 1 2 
> 3 4 
> 
> 3
> 5
> 
> 2 3 
> 5 1 
> 
> 4
> 2
> 
> then do some operation, say add 2 to each element.  Then reassemble them together, to get
> 
>      3     4     5
>      5     6     7
>      4     5     6
>      7     3     4
> What is the most efficient way of doing this?
> Thanks
> Diego

The definition of "efficient" is open to many interpretations ... Here is an example that is not particularly fast, but quite flexible:

% your data
  A = [1 2 3 ; 3 4 5 ; 2 3 4; 5 1 2 ]
  myfun = @(X) 1110 * X(1) + X ;

% engine
  C = mat2cell(A,[2 2],[2 1]) ;
  C = cellfun(myfun, C,'uniformoutput',false) ;
  B = cell2mat(C) 

hth
Jos