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:
Creating sub matrices & taking means

Subject: Creating sub matrices & taking means

From: Matthew Balint

Date: 28 Aug, 2013 19:27:06

Message: 1 of 2

Hello MATLAB community,

I have 105, 400x400 2d arrays. My ultimate goal is to develop a code that will stack the 2d matrices, creating a 400x400x105 3d array, and then use something like blockmean function to create "X" number of sub matrix blocks of just single mean values from the 3d array. In the end I will have single matrix that is, for example, a 100x100x105 matrix (if I were to do a 4x4 blockmean) of just mean values from the original stacked 400x400x105. Stacking them is fine, I am using the cat function, i.e:
        
             X=cat(3,A,B,C,...); %stack matrices

to create my 3d array. This is a little time consuming to write all 105 matrices to stack but I do not know another way?

Since I am new to MATLAB, I have been working with basic code to get me warmed up, so to speak, to this new challenge. I created 3, non-random 10x10 matrices so I could take the mean and calculate this value myself to double check the code:

           A= [1 2 3 4 5 6 7 8 9 10; 1 2 3 4 5 6 7 8 9 10; 1 2 3 4 5 6 7 8 9 10; 1 2 3 4 5 6 7 8 9 10; 1 2 3 4 5 6 7 8 9 10; 1 2 3 4 5 6 7 8 9 10; 1 2 3 4 5 6 7 8 9 10; 1 2 3 4 5 6 7 8 9 10; 1 2 3 4 5 6 7 8 9 10; 1 2 3 4 5 6 7 8 9 10;];

           B= [1 2 3 4 5 6 7 8 9 10; 2 1 3 4 5 6 7 8 9 10; 1 2 3 4 5 6 7 8 9 10; 1 2 3 4 5 6 7 8 9 10; 1 2 3 4 5 6 7 8 9 10; 1 2 3 4 5 6 7 8 9 10; 1 2 3 4 5 6 7 8 9 10; 1 2 3 4 5 6 7 8 9 10; 1 2 3 4 5 6 7 8 9 10; 1 2 3 4 5 6 7 8 9 10;];

           C= [1 2 3 4 5 6 7 8 9 10; 1 2 3 4 5 6 7 8 9 10; 1 2 3 4 5 6 7 8 9 10; 1 2 3 4 5 6 7 8 9 10; 1 2 3 4 5 6 7 8 9 10; 1 2 3 4 5 6 7 8 9 10; 1 2 3 4 5 6 7 8 9 10; 1 2 3 4 5 6 7 8 9 10; 1 2 3 4 5 6 7 8 9 10; 1 2 3 4 5 6 7 8 9 10;];

To get used to picking specific values to create a "block" array and get a blockmean manually, I did:

                 Z=A(7:9,7:9); %creates a single 3x3 array, my "block"

                 J=sum(Z(:))/numel(Z); %gets a total sum, then gets my mean value.

I did the same now for 3d array after stacking these 3 arrays...

                   X1=cat(3,A,B,C); %stack matrices

       P=X1(8:10,8:10,:); %get rows 8 thru 10, columns 8 thru 10, of all dimensions

                  J=sum(P(:)); %sum up all dimensions of P

            F=(J/numel(P)); %find the mean by dividing J by the total # elements of J

 
So great, I got a single mean value for a 3d array. However, my issue is that this approach only works bc it is such a small array. The 400x400 array, I will not be able to pick the values I want, and in the future I will be getting even larger matrices to work with.

I know about the blockmean function for 2d arrays( i.e, Z=blockmean(A,4,4) to get 4x4 sub matrices of the original array, A), as mentioned previously. That is EXACTLY what I want, but for 3d arrays. Does anyone know of a function for 3d arrays that does the exact same thing blockmean does for 2d arrays?

I apologize if this seemed lengthy to ask that question. I just wanted to make sure you all understand the background info of the process I want. Thanks everyone in advance!!!

-Matt

Subject: Creating sub matrices & taking means

From: Matthew Balint

Date: 3 Sep, 2013 14:53:09

Message: 2 of 2

Also, if someone knows how to do this with a loop expression that would be great too. Thanks!

-Matt

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