Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Manipulate a matrix

Subject: Manipulate a matrix

From: Pool

Date: 2 Dec, 2010 15:52:05

Message: 1 of 9

Hi,
how can I manipulate a matrix such as

A =

     1 3 5 6
     2 7 3 1
     5 8 2 4
     1 0 8 6
in order to obtain something like
B = 1 3
          2 7

C = 5 6
          3 1

D = 5 8
          1 0

E = 2 4
          8 6

Is there a function that does this? I don't want to use a for loop!

Thanks in advance for the answer!
Pool


       

Subject: Manipulate a matrix

From: Sean de

Date: 2 Dec, 2010 16:10:23

Message: 2 of 9

"Pool " <paolo.ruggeri@unil.ch> wrote in message <id8fb5$4qd$1@fred.mathworks.com>...
> Hi,
> how can I manipulate a matrix such as
>
> A =
>
> 1 3 5 6
> 2 7 3 1
> 5 8 2 4
> 1 0 8 6
> in order to obtain something like
> B = 1 3
> 2 7
>
> C = 5 6
> 3 1
>
> D = 5 8
> 1 0
>
> E = 2 4
> 8 6
>
> Is there a function that does this? I don't want to use a for loop!
>
> Thanks in advance for the answer!
> Pool
>
>
>

B = A(1:2,1:2)
C = A(1:2,3:4)
D = A(3:4,1:2)
E = A(3:4,3:4)


The real question though is: what are you going to be using these four new matrices for? Is this just a small sample of something that'll create a millions of small matrices?
Please see this:
http://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F

Subject: Manipulate a matrix

From: James Tursa

Date: 2 Dec, 2010 16:25:34

Message: 3 of 9

"Pool " <paolo.ruggeri@unil.ch> wrote in message <id8fb5$4qd$1@fred.mathworks.com>...
> Hi,
> how can I manipulate a matrix such as
>
> A =
>
> 1 3 5 6
> 2 7 3 1
> 5 8 2 4
> 1 0 8 6
> in order to obtain something like
> B = 1 3
> 2 7
>
> C = 5 6
> 3 1
>
> D = 5 8
> 1 0
>
> E = 2 4
> 8 6
>
> Is there a function that does this? I don't want to use a for loop!
>
> Thanks in advance for the answer!
> Pool

For this particular example you could use mat2cell this way:

X = mat2cell(A,[2 2],[2 2]);

And then use X{1,1} etc to get at the sub-matrices.

But I would echo Sean's warning. Why are you doing this?

James Tursa

Subject: Manipulate a matrix

From: Pool

Date: 2 Dec, 2010 16:45:08

Message: 4 of 9

Thank you for your answers!

The question arises from the fact that I'm not able to calculate, for example, the mean of the values of each "sub-matrix". The size of the matrix A that I have chosen is of course too small compared to a real problem matrix.
For real problems, I will probably have to calculate the mean of the values of each 2X2 (or 3X3,....) sub-matrix that compose the original matrix (of size, for example, 60000*60000).


thanks

Subject: Manipulate a matrix

From: Sean de

Date: 2 Dec, 2010 16:57:05

Message: 5 of 9

"Pool " <pool.ruggeri@gmail.com> wrote in message <id8iek$p8r$1@fred.mathworks.com>...
> Thank you for your answers!
>
> The question arises from the fact that I'm not able to calculate, for example, the mean of the values of each "sub-matrix". The size of the matrix A that I have chosen is of course too small compared to a real problem matrix.
> For real problems, I will probably have to calculate the mean of the values of each 2X2 (or 3X3,....) sub-matrix that compose the original matrix (of size, for example, 60000*60000).
>
>
> thanks

If you have the image processing toolbox then:
B = blkproc(A,[2 2],@(x)mean(x(:)))

Else, do James' mat2cell calculation and use cellfun with the above function handle
B = cellfun(@(x)mean(x(:)),X);

Subject: Manipulate a matrix

From: Walter Roberson

Date: 2 Dec, 2010 17:03:14

Message: 6 of 9

On 02/12/10 10:45 AM, Pool wrote:

> The question arises from the fact that I'm not able to calculate, for
> example, the mean of the values of each "sub-matrix". The size of the
> matrix A that I have chosen is of course too small compared to a real
> problem matrix.
> For real problems, I will probably have to calculate the mean of the
> values of each 2X2 (or 3X3,....) sub-matrix that compose the original
> matrix (of size, for example, 60000*60000).

Then you should ask your real question! The question you asked has a
number of different implications than your real question.

To calculate the mean of every submatrix, see blkproc() or blockproc(),
or call cellfun on the cell array returned by mat2cell .

Subject: Manipulate a matrix

From: Matt J

Date: 2 Dec, 2010 17:08:05

Message: 7 of 9

"Sean de " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <id8j51$k0d$1@fred.mathworks.com>...
>
> If you have the image processing toolbox then:
> B = blkproc(A,[2 2],@(x)mean(x(:)))
>
> Else, do James' mat2cell calculation and use cellfun with the above function handle
> B = cellfun(@(x)mean(x(:)),X);
============

Even if you have the image processing toolbox, avoid blkproc. It's much slower than the cellfun approach and is mainly meant for block-processing data held in files.

Subject: Manipulate a matrix

From: Pool

Date: 2 Dec, 2010 17:21:22

Message: 8 of 9

"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <id8jpl$29h$1@fred.mathworks.com>...
> "Sean de " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <id8j51$k0d$1@fred.mathworks.com>...
> >
> > If you have the image processing toolbox then:
> > B = blkproc(A,[2 2],@(x)mean(x(:)))
> >
> > Else, do James' mat2cell calculation and use cellfun with the above function handle
> > B = cellfun(@(x)mean(x(:)),X);
> ============
>
> Even if you have the image processing toolbox, avoid blkproc. It's much slower than the cellfun approach and is mainly meant for block-processing data held in files.


Thank you for your answers!

Subject: Manipulate a matrix

From: James Tursa

Date: 2 Dec, 2010 18:13:04

Message: 9 of 9

"Pool " <pool.ruggeri@gmail.com> wrote in message <id8iek$p8r$1@fred.mathworks.com>...
> Thank you for your answers!
>
> The question arises from the fact that I'm not able to calculate, for example, the mean of the values of each "sub-matrix". The size of the matrix A that I have chosen is of course too small compared to a real problem matrix.
> For real problems, I will probably have to calculate the mean of the values of each 2X2 (or 3X3,....) sub-matrix that compose the original matrix (of size, for example, 60000*60000).

Any m-code based calculation that involves forming the 2x2 or 3x3 etc slices explicitly will have a tremendous time (and possibly memory) penalty. e.g., forming each 2x2 slice explicitly involves copying 4 elements *plus* an extra 100 bytes or so of variable structure overhead. Now multiply that overhead by the actual sizes involved in your large matrix and you can see the tremendous waste. To avoid this tremendous time/memory penalty you would need a method that does not explicitly form the 2x2 slice (e.g., a built-in function if available or a mex routine). Is your original matrix sparse? Your 60000x60000 example would be a 27GB array if full double. Even if you have the memory to store this size of an array in RAM, the mat2cell method posted earlier would certainly blow your memory completely out of the water, since each 2x2 slice would bloat from 2x2x8 = 32 bytes to 100+ bytes,
tripling or quadrupling the memory footprint if all those slices are in memory at the same time. e.g.,

>> A = rand(1000,1000);
>> X = mat2cell(A,2*ones(500,1),2*ones(500,1));
>> whos
  Name Size Bytes Class Attributes
  A 1000x1000 8000000 double
  X 500x500 23000000 cell

>> A(1:2,1:2)
ans =
    0.9040 0.9278
    0.9409 0.9959

>> X{1,1}
ans =
    0.9040 0.9278
    0.9409 0.9959



James Tursa

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us