简体   繁体   中英

Calculation mean across dimensions

Beginners MATLAB question I have 4 variables(beam1,beam2,beam3,beam4) in separate arrays with the same dimensions (23746 x 35). I want to calculate the average of the 4 arrays to have an answer in one array with the same size (23746 x 35). In other words I want to average the values in the first dimension.

x = beam1,beam2,beam3,beam4 xx = mean(x,1)

Gives a 1x35 array and

x = beam1,beam2,beam3,beam4 xx = mean(x,2)

Gives a 23746x1 array

How do I keep the same dimensions (23746 x 35) in the new array and compute the average value of the 4 individual arrays?

Many thanks

Doesn't simply doing

xx = (beam1 + beam2 + beam3 + beam4)/4

give what you want?

Note that it's typically bad practice to have variable names that only differ by an appended integer such as you have. You've be better creating the data as a 3-dimenional array where

beam_data = beam1;
beam_data(:,:,2) = beam2;
beam_data(:,:,3) = beam3;
beam_data(:,:,4) = beam4;

In this way if you have more (or less) beams then you don't have a proliferation of variables.

It also means irrespective of how many beams you have the mean would be calculates along the 3rd dimension

beam_mean = mean(beam_data,3);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM