Mean of 3D matrix based on range of first dimension

23 ビュー (過去 30 日間)
John Cruce
John Cruce 2025 年 9 月 11 日 19:15
編集済み: Matt J 2025 年 9 月 11 日 21:25
I have a 3d matrix of size (365,360,720) with daily half degree global temperature data. I'd like to take the average over a range of days, say 100-200. So effectively I'd like a mean (100:200,:,:) into a matrix of size (360,720). I'm stuck on how to quickly do this. Thanks.

採用された回答

Star Strider
Star Strider 2025 年 9 月 11 日 19:34
Perhaps something like this --
A = randn(360,360,720);
At = A(100:200,:,:);
Atm = mean(At, 1);
AtmSize = size(Atm)
AtmSize = 1×3
1 360 720
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Atm = squeeze(Atm)
Atm = 360×720
-0.0563 -0.0387 -0.0523 0.0762 -0.1329 -0.0509 0.0523 0.0332 -0.2267 -0.1836 -0.0127 -0.0402 -0.0735 0.1392 0.0235 0.0728 -0.0159 0.1024 0.0071 0.0243 -0.0520 -0.0282 0.0572 -0.0392 -0.0686 0.0673 0.0416 0.0407 0.0097 0.0737 0.0469 -0.0999 0.0627 0.0330 -0.0786 -0.0207 -0.0271 -0.1168 0.0365 0.1092 0.0897 0.0962 0.1419 0.0330 -0.0173 0.1815 -0.0110 0.0431 0.1018 -0.0074 -0.0927 -0.1060 0.0711 -0.0474 0.1863 0.1030 -0.0059 0.0058 0.0447 -0.0826 0.1906 0.0866 -0.2160 -0.2412 0.1257 0.1013 -0.0033 -0.1745 -0.0701 0.0379 -0.0344 0.0819 0.0688 -0.0467 0.1903 -0.0803 0.2209 0.2378 -0.0555 0.0585 -0.0532 -0.0156 -0.0321 0.1816 0.0592 0.0265 -0.2052 -0.0298 -0.0451 0.0030 -0.0708 0.0826 0.1403 -0.0555 -0.0678 0.0524 -0.0569 0.1118 -0.0676 -0.0215 0.1798 -0.1957 -0.0228 -0.1010 -0.1025 -0.1472 0.0396 -0.0681 -0.0649 -0.0266 0.1162 0.1447 0.1257 -0.0216 -0.0912 0.0638 0.0584 -0.0496 -0.1483 0.0754 -0.0800 -0.0840 0.1532 -0.0463 0.0745 -0.0121 -0.1472 -0.0364 -0.3824 -0.0633 -0.0319 -0.0034 0.0276 -0.0376 0.0569 -0.0032 0.1157 -0.0350 -0.1528 0.0798 -0.0070 0.0010 0.0776 0.0248 0.2047 0.0146 -0.0516 -0.1305 0.1231 -0.1489 0.0909 0.0122 0.0778 0.1473 -0.0347 -0.0859 0.1104 -0.1017 -0.0618 -0.1294 0.1392 0.1307 0.0435 -0.0578 0.0485 0.0177 0.0465 0.0444 0.1278 -0.0675 -0.0554 0.0631 0.0257 -0.0960 0.1843 -0.2281 -0.0226 -0.0779 -0.0059 0.0229 0.1359 0.0326 0.1371 -0.0836 0.0047 0.0734 0.0038 0.0420 -0.0356 -0.0251 0.1240 0.0632 -0.0085 -0.1897 0.0406 0.1528 -0.1264 -0.0851 -0.0461 -0.1030 -0.0925 0.2176 0.0395 0.2468 0.0988 0.0961 -0.0090 0.1564 0.0624 -0.0553 0.1334 -0.2958 -0.0802 -0.0781 0.0857 -0.0047 -0.1466 0.0133 -0.0773 -0.1473 -0.0936 0.1276 -0.0578 -0.0330 -0.0075 0.0237 0.0484 -0.0033 -0.1414 0.0139 0.1428 0.0325 0.1843 -0.0210 -0.0465 0.0411 -0.0249 0.1968 -0.1111 0.0148 0.0386 -0.1297 -0.0755 0.1352 0.0123 -0.1832 -0.0537 -0.1020 -0.0122 0.0468 -0.1295 -0.1598 0.1508 -0.0296 -0.1561 -0.0962 -0.1094 0.0313 0.0405 -0.0062 -0.0712 0.0468 0.1133 -0.1454 0.0240 0.0102 0.1901 0.1144 -0.0110 -0.0791 -0.0127 0.0958 -0.0837 -0.1954 -0.0482 0.0117 0.1084 -0.1303 -0.0329 0.0282 0.2331 0.0007 -0.0029 -0.0874 0.0730 -0.0068 -0.0071 -0.1582 -0.1228 -0.0003 0.1739 -0.1212 -0.0658 0.0427 -0.0349 0.1073 -0.1038 0.2022 -0.0739 -0.1452 0.1411 -0.0730 0.0093 0.0812 -0.0779 -0.0931 0.1126 0.0753 -0.1362 0.0440 -0.1879 -0.0543 -0.1332 0.1606 0.0242 -0.1093 0.1185 -0.1820 -0.0239 -0.2374 0.1241 0.0591 0.1297 0.1133 0.1033 -0.0727 -0.1683 0.0495 0.0288 0.1134 0.2239 -0.1604 -0.0833 -0.0084 0.0000 0.2061 0.0766 -0.0148 0.1127 -0.1086 -0.0613 0.0319 0.0190 -0.2960 -0.0999 -0.0719 0.0750 -0.0828 -0.1119 0.0608 -0.0867 0.1495 -0.1421 -0.0346 -0.0366 -0.1248 -0.0670 -0.1580 0.1969 0.1194 0.0338 0.0934 -0.0507 0.0570 0.1196 0.0105 -0.0177 -0.0464 0.0478 -0.1821 0.0919 -0.1310 -0.1995 0.0044 0.0293 0.0679 0.1780 0.0508 -0.0188 0.1694 0.1436 -0.0651 0.0600 0.0862 -0.0105 -0.0182 0.0309 -0.1479 -0.1018 -0.1670 0.1421 -0.0875 0.0314 0.0523 0.1086 -0.0037 -0.0581 -0.0768 -0.1244 0.0780 0.0105 -0.0382 0.0635 0.0418 0.0433 -0.2012 0.0198 -0.1365 0.0818 -0.0022 0.0028 0.1736 -0.0620 0.1086 -0.1206 -0.0350 -0.0608 0.1143 -0.0987 -0.0882 -0.0075 0.0770 0.1313 -0.0606 -0.0470 -0.0827 -0.0535 -0.0111 0.1745 -0.0278 0.0906 0.0134 0.0343 -0.0381 -0.1171 -0.0575 0.2197 0.0188 0.1222 -0.1152 0.0107 0.0537 -0.0222 0.0070 -0.0486 -0.0998 0.0843 -0.1137 -0.0296 0.0089
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
AtmSize = size(Atm)
AtmSize = 1×2
360 720
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
.

その他の回答 (1 件)

Matt J
Matt J 2025 年 9 月 11 日 21:16
編集済み: Matt J 2025 年 9 月 11 日 21:24
This might seem a little counter-intuitive, but the key is that it avoids extracting a submatrix (an expensive op) from the input matrix A,
s=zeros(1,365); s(100:200)=1; s=s/sum(s);
result = reshape(s*A(:,:),360,720);
  1 件のコメント
Matt J
Matt J 2025 年 9 月 11 日 21:20
編集済み: Matt J 2025 年 9 月 11 日 21:25
A short execution time comparison:
A = randn(365,360,720);
timeit(@()methodStandard(A))
ans = 0.1238
timeit(@()methodProposed(A))
ans = 0.0075
function methodStandard(A)
At = A(100:200,:,:);
result = reshape(mean(At, 1),360,720);
end
function methodProposed(A)
s=zeros(1,365); s(100:200)=1; s=s/sum(s);
result = reshape(s*A(:,:),360,720);
end

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

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by