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:
4D array manipulation

Subject: 4D array manipulation

From: edward kabanyas

Date: 12 Jul, 2011 05:15:12

Message: 1 of 7

Dear friends,

I am really beginner for 4D array. Currently I am working with 4D array, assuming the file name is A; size(A)
ans =

   140 73 17 388

Number of rows = 140
number of column=73
number of dim3 = 17
number of dim4 = 388

I want to calculate the mean of the data in:

rows: 37-61 and column 33-41 for each dim3 and for all dim4. Hence the step probably:

1) Average the data with rows: 37, column 33-41 for dim3=1 and for all dim4 (0...388).
2) Average the data with rows: 38, column 33-41 for dim3=2 and for all dim4 (0...388).
....
n)Average the data with rows: 61, column 33-41 for dim3=17 and for all dim4 (0...388).

The last output is only 2D array. Any help, I really appreciate it.

Best regards,
Edward

Subject: 4D array manipulation

From: edward kabanyas

Date: 12 Jul, 2011 06:22:09

Message: 2 of 7

Dear Friends,

Probably it is difficult to understand my first message. Therefore to make more clear, I will re-write it:

Assuming my data is 4D as A, size(A)
ans =

   140 73 17 388

Number of rows = 140 (longitude)
number of column=73 (latitude)
number of dim3 = 17 (high)
number of dim4 = 388 (time)

I want to calculate the mean of the data in:

each longitude within row 37-61 and for all latitude within column 33-41 for each dim3 and for all dim4 (1...388). Hence, each grid of longitude, for each high, for all latitude and time, I will have their mean.

Therefore, the last output is only 2D array, the mean value of the data for each latitude versus high. Again any help, I really appreciate it.

Best regards,
Edward

Subject: 4D array manipulation

From: edward kabanyas

Date: 12 Jul, 2011 06:44:09

Message: 3 of 7


Dear Friends,

Again, I add an example of my question. Let's see simple 4D array with dimension [3 3 1 4]:

 B = cat( 4, [2 8 3; 0 5 3; 2 8 3], [1 3 4; 7 9 4; 2 8 3], [2 3 4; 4 6 5; 2 8 3], [2 3 4; 4 6 5; 2 8 3])

B(:,:,1,1) =

     2 8 3
     0 5 3
     2 8 3


B(:,:,1,2) =

     1 3 4
     7 9 4
     2 8 3


B(:,:,1,3) =

     2 3 4
     4 6 5
     2 8 3


B(:,:,1,4) =

     2 3 4
     4 6 5
     2 8 3

My question is : How to calculate average of each rows 2 and 3 for column 2-3 and all dim4. If we take from the above array B, my question is:

Mean_1 (for row 2)= ((5 + 3)+( 9 + 4)+(6 + 5)+(6 + 5))/8 = 5.3750;
Mean_2 (for row 3)= (( 8 + 3)+(8 + 3)+( 8 + 3)+( 8 + 3))/8= 5.5000;

Final output is Mean_1, Mean_2. This for dim3 = 1, for my case dim3 =17. But in general, the example of my case is close to this example. Again, thanks for help.

Edward

Subject: 4D array manipulation

From: Bruno Luong

Date: 12 Jul, 2011 06:52:10

Message: 4 of 7

>
> My question is : How to calculate average of each rows 2 and 3 for column 2-3 and all dim4. If we take from the above array B, my question is:

In your example, run this will return the expected average:

 mean(mean(B(2:3,2:3,:,:),2),4)

Feel free to adapt the code if the third dimension is not a singleton.

% Bruno

Subject: 4D array manipulation

From: edward kabanyas

Date: 12 Jul, 2011 08:10:26

Message: 5 of 7

Dear Bruno;

> mean(mean(B(2:3,2:3,:,:),2),4)

It is great, work for my example. However, would you mind to show me for my real case ?

Assuming my data is 4D as A, size(A)
ans =

   140 73 17 388

Number of rows = 140 (longitude)
number of column=73 (latitude)
number of dim3 = 17 (high)
number of dim4 = 388 (time)

I want to calculate the mean of the data in:

each longitude within row 37-61 and for all latitude within column 33-41 for each dim3 and for all dim4 (1...388). Hence, each grid of longitude, for each high, for all latitude and time, I will have their mean.

I use your code as:

mean(mean(A(37:61,33:41,:,:),2),4), However, I am not sure about the result because I can not check the result manually because the data is very big.

When I chek the size of A(37:61,33:41,:,:) it is:

ans =

    25 9 17 388

Again, thanks for your help

Edward

Subject: 4D array manipulation

From: Bruno Luong

Date: 12 Jul, 2011 08:29:10

Message: 6 of 7

"edward kabanyas" <djuky_hmi@yahoo.com> wrote in message <ivgvhi$f9h$1@newscl01ah.mathworks.com>...
> However, I am not sure about the result because I can not check the result manually because the data is very big.

Then instead for manual check, just write a check using for-loop or whatever way that you might know. That shouldn't be that hard.

Bruno
 

Subject: 4D array manipulation

From: edward kabanyas

Date: 13 Jul, 2011 01:17:27

Message: 7 of 7

Hi Brono !

Thank you for your reply.

> Then instead for manual check, just write a check using for-loop or whatever way that you might know. That shouldn't be that hard.

I have check using looping procedure and your code works well by modifying:

mean_A=mean(mean(A(37:61,33:41,:,:),2),4);
mean_A=squeeze(A);
 
 
Again, thank you

Edward

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