フィルターのクリア

mean of all these variables over latitude x longitude x time that is (5 x 5 x 2)

13 ビュー (過去 30 日間)
Devendra
Devendra 2023 年 6 月 30 日
回答済み: Cris LaPierre 2023 年 7 月 20 日
この 質問 は Cris LaPierre さんによってフラグが設定されました
I have eight variables loaded with data (latitude,longitude,time,months) as (5,5,2,65). I want to calculate the mean of all these variables over latitude x longitude x time that is (5 x 5 x 2). My output should have the dimensions of (65,8), that is mean values of 8 variables for 65 months.
I would be highly obliged to get fews lines of matlab code to do it.
regards,
Devendra
  3 件のコメント
Devendra
Devendra 2023 年 7 月 1 日
How to aviod NaN numvers in data set?
Thanks.
Devendra
Stephen23
Stephen23 2023 年 7 月 1 日
"How to aviod NaN numvers in data set?"
Use the OMITNAN flag.

サインインしてコメントする。

採用された回答

Prannoy
Prannoy 2023 年 6 月 30 日
編集済み: Prannoy 2023 年 6 月 30 日
To calculate the mean of the variables over the dimensions (latitude x longitude x time) and obtain an output with dimensions (65, 8), you can use the mean function in MATLAB along with appropriate reshaping and transpose operations. Here's an example code snippet that demonstrates this:
% Calculate the mean over latitude x longitude x time
mean_values = mean(reshape(variable1, [], 5, 5, 2), [2, 3]);
Unrecognized function or variable 'variable1'.
% Reshape the mean values to have dimensions (65, 8)
mean_values = reshape(mean_values, 65, []);
% Concatenate the mean values with the other variables
output = [mean_values, variable2, variable3, variable4];
% Display the output matrix
disp(output);
In this code, variable1 represents the first variable for which you want to calculate the mean. You can repeat the process for the other variables by replacing variable1 with the corresponding variable names (variable2, variable3, etc.).

その他の回答 (4 件)

Arya Chandan Reddy
Arya Chandan Reddy 2023 年 6 月 30 日
Hi Devendra, I understand that you are trying to calculate mean of your data (5x5x2) over 65 months for each of the 8 variables.
Assuming that you data is in the format 5x5x2x65x8
Here is the code:
x = rand([5 5 2 65 8]);
m = mean(x , [1 2 3]);
m = reshape(m , [65 8]);
Refer to the documentation of mean and reshape for better understanding.
Hope it helps
  2 件のコメント
Devendra
Devendra 2023 年 6 月 30 日
data format for each variable is (5,5,2,65) not (5,5,2,65,8) as presumed by you and there are 8 variables
Stephen23
Stephen23 2023 年 7 月 1 日
編集済み: Stephen23 2023 年 7 月 1 日
+1 tidy solution, neat use of RESHAPE. Using one array is good advice,
"data format for each variable is (5,5,2,65) not (5,5,2,65,8) as presumed by you and there are 8 variables"
You can simply use CAT to join them together:
x = cat(5,A1,A2,..,A8);
Even better is to not have eight separate variables in the first place.

サインインしてコメントする。


DGM
DGM 2023 年 6 月 30 日
編集済み: DGM 2023 年 6 月 30 日
If your data is a bunch of individual numeric arrays:
% some fake data
x1 = rand(5,5,2,65);
x2 = rand(5,5,2,65);
x3 = rand(5,5,2,65);
x4 = rand(5,5,2,65);
x5 = rand(5,5,2,65);
x6 = rand(5,5,2,65);
x7 = rand(5,5,2,65);
x8 = rand(5,5,2,65);
allmeans = [squeeze(mean(x1,1:3)) ...
squeeze(mean(x2,1:3)) ...
squeeze(mean(x3,1:3)) ...
squeeze(mean(x4,1:3)) ...
squeeze(mean(x5,1:3)) ...
squeeze(mean(x6,1:3)) ...
squeeze(mean(x7,1:3)) ...
squeeze(mean(x8,1:3))];
Alternatively,
% or maybe use a cell array
C = {x1 x2 x3 x4 x5 x6 x7 x8};
allmeans = cellfun(@(x) squeeze(mean(x,1:3)),C,'uniform',false);
allmeans = cell2mat(allmeans);
  2 件のコメント
Devendra
Devendra 2023 年 6 月 30 日
data contains NaN values. How to avoid NaN values.
DGM
DGM 2023 年 6 月 30 日
編集済み: DGM 2023 年 6 月 30 日
If you want to simply ignore the NaNs, you can use the 'omitnan' flag in the calls to mean().
mu = mean([1 2 3 4 NaN],'omitnan')
mu = 2.5000

サインインしてコメントする。


Peter Perkins
Peter Perkins 2023 年 7 月 17 日
It's very likely that you should be using a timetable.
You have "eight variables loaded with data (latitude,longitude,time,months) as (5,5,2,65)". I think you mean that you have 8 5x5x2x65 arrays. Here's what I recommend:
create the lat/lon/time/month values as 5/5/2/65-element vectors, respectively. Use ndgrid to expand those out to four 5x5x2x65 matrices, where each one of them has a lot of repeated lat/lon/time/month values. Turn those four "coordinate" arrays and your eight data arrays into columns using (:), and put those into a (5*5*2*65)-by-(4+8) table. Now call groupsummary to compute monthy means, or month-by-time means, or lat-by-lon means.
Depending on what timestamps you have, you may want to look at using datetimes, or durations, and putting your data in a timetable. Not enough info to go on.

Cris LaPierre
Cris LaPierre 2023 年 7 月 20 日

カテゴリ

Help Center および File ExchangeNumeric Types についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by