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:
Using loop to Cube data

Subject: Using loop to Cube data

From: Matthew Balint

Date: 13 Sep, 2013 20:03:06

Message: 1 of 8

Hi MATLAB community,

I have 105, 400x400 arrays. I want to stack these arrays on top of each other, in any number of ways, i.e, 400x400x3, 400x400x10, 400x400x105....

The main idea is that they will be a 3D array.

Essentially what needs to be done, is after I stack them, let's say in a 400x400x3 array, I need to write a loop that will break the array into cubes (basically sub matrices) of my specification. For example, take my 400x400x3 array and break this by [4,4] blocks where the output is a single matrix of 100x100, where each number of the new 100x100 is the summation value of the numbers in each [4,4] block. Is that clear?

Here is my code.

A=layer6 %my 400x400 matrix
B=layer7 %my 400x400 matrix
C=layer8 %my 400x400 matrix

X=cat(3,A,B,C); %my concatenated 400x400x3 matrix

%this is the tricky part where my code is obviously going wrong.
for i=1,100; %my new matrix size??
    j=1,100; %my new matrix size??
    k=1:3; %my new matrix demensions to loop thru??
    ii=1+(i-1)*4; %making the 4x4 blocks??
    jj=j+(j-1)*4; %making the 4x4 blocks??
    D(i,j,k)=mean(mean(X(ii:ii+4,jj:jj+4,1:3))); %my new matrix of demensions 100x100???
end


Please help!!!! I don't know why this won't work. I get an error message saying subscript assignment mismatch!?? I'm relatively new to MATLAB so please be nice! Thanks in advance for all and any help!

-Matt

Subject: Using loop to Cube data

From: dpb

Date: 13 Sep, 2013 20:20:35

Message: 2 of 8

On 9/13/2013 3:03 PM, Matthew Balint wrote:
...

> ii=1+(i-1)*4; %making the 4x4 blocks??
> jj=j+(j-1)*4; %making the 4x4 blocks??
> D(i,j,k)=mean(mean(X(ii:ii+4,jj:jj+4,1:3)));
...

> Please help!!!! I don't know why this won't work. I get an error message
> saying subscript assignment mismatch!?? ...

'Cuz D(i,j,k) is a single element of an array D and you're trying to
assign a 4x4x3 array into that one element--doesn't fit.

To do the way you're doing you need sotoo

D(i:i+3,j:+3,:) = rhs...;

I'm late for other engagement so don't have time but if have image
processing toolbox, look at blockproc().

Otherwise look at arrayfun() as an alternative

--

Subject: Using loop to Cube data

From: james bejon

Date: 13 Sep, 2013 20:50:15

Message: 3 of 8

Not 100% sure what you're after here. Here's an example of the kind of reshaping/permuting you might want to use:

% Data
x = reshape( 1:100, [10 10] );

% Slice into [2x2] layers
y = permute( reshape(x, [2 5 2 5]), [1 3 2 4] );

% Sum each layer, to get a [5x5]
squeeze(sum(sum(y)))

Subject: Using loop to Cube data

From: Matthew Balint

Date: 16 Sep, 2013 13:50:15

Message: 4 of 8

dpb <none@non.net> wrote in message <l0vs32$ft4$1@speranza.aioe.org>...
> On 9/13/2013 3:03 PM, Matthew Balint wrote:
> ...
>
> > ii=1+(i-1)*4; %making the 4x4 blocks??
> > jj=j+(j-1)*4; %making the 4x4 blocks??
> > D(i,j,k)=mean(mean(X(ii:ii+4,jj:jj+4,1:3)));
> ...
>
> > Please help!!!! I don't know why this won't work. I get an error message
> > saying subscript assignment mismatch!?? ...
>
> 'Cuz D(i,j,k) is a single element of an array D and you're trying to
> assign a 4x4x3 array into that one element--doesn't fit.
>
> To do the way you're doing you need sotoo
>
> D(i:i+3,j:+3,:) = rhs...;
>
> I'm late for other engagement so don't have time but if have image
> processing toolbox, look at blockproc().
>
> Otherwise look at arrayfun() as an alternative
>
> --

I tried to use the blockprock function, and I don't understand the 'fun' function handle. It instructed me to use 'block_struct' and 'block_struct.data' and I have no idea what those are or how to use them. My array is simply numeric and I thought block_struct.data was an array that contained multiple values, not just numeric values? Please help and thanks in advance!

-Matt

Subject: Using loop to Cube data

From: Steven_Lord

Date: 16 Sep, 2013 15:25:26

Message: 5 of 8



"Matthew Balint" <mjbalint2@gmail.com> wrote in message
news:l172an$2gh$1@newscl01ah.mathworks.com...
> dpb <none@non.net> wrote in message <l0vs32$ft4$1@speranza.aioe.org>...
>> On 9/13/2013 3:03 PM, Matthew Balint wrote:

*snip*

> I tried to use the blockprock function, and I don't understand the 'fun'
> function handle.

It is the function that you want to be applied to each block.

> It instructed me to use 'block_struct' and 'block_struct.data' and I have
> no idea what those are or how to use them.

See the Definitions section of the BLOCKPROC documentation page for
information about the fields contained in the block_struct struct array.

http://www.mathworks.com/help/images/ref/blockproc.html

My guess is that BLOCKPROC passes that structure instead of just the
specified piece of your matrix into the function because for image
processing applications that extra information may be useful. The data field
of that struct array contains just the specified piece of your matrix.

> My array is simply numeric and I thought block_struct.data was an array
> that contained multiple values, not just numeric values?

Take a look at this example:

>> M = reshape(1:16, [4 4]);
>> blockproc(M, [2 2], @(block_struct) mean(block_struct.data(:)))

BLOCKPROC will call this function four times: once with the upper left
2-by-2 block of M stored in block_struct.data, once with the upper-right,
once with the lower-left, and once with the lower-right.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Using loop to Cube data

From: Matthew Balint

Date: 17 Sep, 2013 19:39:10

Message: 6 of 8

> See the Definitions section of the BLOCKPROC documentation page for
> information about the fields contained in the block_struct struct array.
>
> http://www.mathworks.com/help/images/ref/blockproc.html
>
> My guess is that BLOCKPROC passes that structure instead of just the
> specified piece of your matrix into the function because for image
> processing applications that extra information may be useful. The data field
> of that struct array contains just the specified piece of your matrix.
>
> > My array is simply numeric and I thought block_struct.data was an array
> > that contained multiple values, not just numeric values?
>
> Take a look at this example:
>
> >> M = reshape(1:16, [4 4]);
> >> blockproc(M, [2 2], @(block_struct) mean(block_struct.data(:)))
>
> BLOCKPROC will call this function four times: once with the upper left
> 2-by-2 block of M stored in block_struct.data, once with the upper-right,
> once with the lower-left, and once with the lower-right.
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com


Thanks for the reply... does that mean blockproc it only goes to those 4 places you mentioned--upper left-right-lower left-right-- and not through the entire matrix to create blocks? I am currently researching this and I am getting different answers from every different site I look at. Thanks in advance for all of your help.

Subject: Using loop to Cube data

From: dpb

Date: 17 Sep, 2013 21:20:03

Message: 7 of 8

On 9/17/2013 2:39 PM, Matthew Balint wrote:
...

> Thanks for the reply... does that mean blockproc it only goes to those 4
> places you mentioned--upper left-right-lower left-right-- and not
> through the entire matrix to create blocks? I am currently researching
> this and I am getting different answers from every different site I look
> at. Thanks in advance for all of your help.

Why don't you go to the source? What do you need any other site than
the TMW documentation?

" ...
B = blockproc(A,[M N],fun) processes the image A by applying the
function fun to each distinct M-by-N block of A and concatenating
the results into B, the output matrix. fun is a function handle ..."

If you're still uncertain, create a small matrix of known values that
you can hand compute the result and try it and see what you get...

A=reshape([1:16],4,4); % would be a likely candidate for 2x2 processing

--

Subject: Using loop to Cube data

From: Steven_Lord

Date: 18 Sep, 2013 14:20:08

Message: 8 of 8



"Matthew Balint" <mjbalint2@gmail.com> wrote in message
news:l1ab4u$n4e$1@newscl01ah.mathworks.com...
>> See the Definitions section of the BLOCKPROC documentation page for
>> information about the fields contained in the block_struct struct array.
>>
>> http://www.mathworks.com/help/images/ref/blockproc.html
>>
>> My guess is that BLOCKPROC passes that structure instead of just the
>> specified piece of your matrix into the function because for image
>> processing applications that extra information may be useful. The data
>> field of that struct array contains just the specified piece of your
>> matrix.
>>
>> > My array is simply numeric and I thought block_struct.data was an array
>> > that contained multiple values, not just numeric values?
>>
>> Take a look at this example:
>>
>> >> M = reshape(1:16, [4 4]);
>> >> blockproc(M, [2 2], @(block_struct) mean(block_struct.data(:)))
>>
>> BLOCKPROC will call this function four times: once with the upper left
>> 2-by-2 block of M stored in block_struct.data, once with the upper-right,
>> once with the lower-left, and once with the lower-right.
>>
>> --
>> Steve Lord
>> slord@mathworks.com
>> To contact Technical Support use the Contact Us link on
>> http://www.mathworks.com
>
>
> Thanks for the reply... does that mean blockproc it only goes to those 4
> places you mentioned--upper left-right-lower left-right-- and not through
> the entire matrix to create blocks? I am currently researching this and I
> am getting different answers from every different site I look at. Thanks
> in advance for all of your help.

Well, in a 4-by-4 matrix there are only four nonoverlapping 2-by-2 blocks:
upper left, upper right, lower left, and lower right. If you tried this with
a 6-by-6 matrix there would be nine blocks: upper left, upper center, upper
right, center left, etc. For a 10-by-20 the result would be 5-by-10, but I'm
not going to try to come up with names for each of those positions ;)

M = reshape(1:36, [6 6]);
blockproc(M, [2 2], @(block_struct) mean(block_struct.data(:)))

You may also find the first half or so of this posting on the other Steve's
blog illuminating:

http://blogs.mathworks.com/steve/2011/08/16/dealing-with-really-big-images-block-processing/

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

No tags are associated with 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