for loop with three variables in 3d matrix

13 views (last 30 days)
Jacqui
Jacqui on 28 Oct 2021
Edited: DGM on 28 Oct 2021
Hi everyone,
I am trying to run a for loop for three variables for data that is stored in a 3d matrix. I have three data sheets stored in a 3d matrix called 'data'. Each matrix has 8760 rows and 20 columns. The different sheets in the matrix represent different wall constructions. I need 4 columns of each sheet which represent the RH (relative Humidity) of a material surface.
I want to know, how often the RH in each column (material layer), in each sheet (construction), exceeds 99 (%). (count)
In the End, I need one matrix showing the results together where the columns represent the different marital layer (4) and the rows represent the constructions (3).
I'm struggling to separate and output the data.
So far I defined:
RH_ = data( : , [21, 23, 25, 27], [1:3] )
lengthRH_=length(RH_) % 8760 timesteps/hours in this case
results = zeros( 3, 4)
critical_RH = zeros((lengthRH_), 4, 3)
for m = 1 : lengthRH_ % m = variable values in each row
for j = 1 : 4 % j = variable collumn
for i = 1:3 % i = variable construction
if RH_(m, j, i) > 99
critical_RH(m, j, i) = 1;
end
end
end
index = find(critical_RH(m,j,i) == 1);
RH_excess_days = length(index)./24
end
It would be amazing if someone has an answer for me. :)
Thanks a lot
Jacqui

Answers (1)

DGM
DGM on 28 Oct 2021
Try
RH_ = 50 + 50*rand(100,4,3) % example data
RH_ =
RH_(:,:,1) = 75.3767 57.7778 55.2978 55.7742 93.0968 60.4116 95.4869 62.4389 65.4675 64.2716 54.6249 54.6762 70.6377 87.3722 61.0153 89.2603 57.6246 75.5108 96.7440 79.6598 88.6035 68.6012 90.7596 64.2951 59.6499 71.7493 59.0893 63.0074 81.1525 58.5801 82.0468 96.1789 84.5913 90.5598 50.5077 86.5831 81.8153 97.6012 78.3028 85.9282 65.9561 54.6319 67.1231 89.9449 66.4165 86.2648 85.0503 85.7359 55.5568 86.7174 67.8338 69.8330 90.6434 53.3019 87.0969 92.0268 55.5527 59.5231 67.9808 66.9193 74.1190 93.9039 90.6552 98.3995 51.1867 73.2341 53.2158 87.9720 70.3266 91.2271 92.0813 85.7711 94.1340 61.2872 76.0385 78.0140 84.4244 50.5369 87.0865 71.6125 54.2807 74.1922 55.2478 56.2021 66.5661 95.0648 93.6797 93.2684 75.4452 51.9125 77.6613 66.3999 75.6852 80.4529 56.7501 74.0525 94.9581 84.7867 77.0851 80.0300 81.5504 62.7438 52.4737 92.8519 59.2073 92.1576 58.6649 76.6641 86.0940 52.9715 69.9629 75.7703 66.0982 77.4446 57.6634 82.8808 88.0112 59.2829 61.2382 53.9113 78.2231 56.0075 95.8743 66.9739 98.5949 55.0104 93.6340 79.0117 84.0981 67.3273 74.3884 63.0780 55.0503 93.5175 99.1246 55.5351 92.3407 60.4917 83.1172 76.2926 78.8463 83.9264 78.0350 86.5037 63.2574 99.9129 77.2496 92.6783 92.6705 84.9733 79.4826 50.8842 57.3201 94.9408 74.4713 53.4181 56.4851 81.9468 74.4119 96.3463 84.5807 88.0857 50.5033 72.2741 86.5550 61.0615 61.7926 87.7465 98.3929 58.3328 89.7887 79.3834 80.9634 68.6995 63.9407 53.5106 70.7190 52.0189 81.1755 76.5903 84.3220 98.3456 93.7380 74.5734 77.8415 99.8150 96.3213 82.1715 74.2801 57.6192 52.1849 73.7205 66.7051 59.1682 88.8083 53.2485 92.6999 55.6121 82.0099 50.9576 59.6396 63.5841 64.8450 99.7235 70.4165 58.3396 65.3789 88.8991 63.2987 77.7938 90.8592 63.6866 90.2452 76.0015 78.8464 85.9073 97.9822 99.9517 96.9393 73.4707 57.6885 64.2791 55.1464 94.6497 55.3633 52.1926 92.0574 69.5505 58.5065 56.3400 76.1587 63.8832 67.5803 75.3725 61.4424 81.6932 67.8349 89.0146 82.7236 81.6749 89.8428 88.0300 87.3042 99.3779 61.1677 61.3931 50.9598 89.0384 98.5049 80.3174 54.2705 51.6864 56.6510 87.4682 79.7591 89.7376 56.5765 56.8598 93.1831 77.8626 71.2125 92.0594 56.1428 78.0488 64.5748 54.2061 62.3666 82.8313 91.8492 88.6709 70.9999 99.2383 68.5928 88.2330 70.4238 56.3251 60.7039 63.8036 56.9108 75.7503 89.6592 93.6810 88.4113 70.6487 77.4679 66.0152 82.6691 95.4397 90.2254 52.9307 98.8568 70.6350 67.2721 56.3469 91.3834 61.6966 65.5050 76.1526 97.4177 73.2824 75.5228 93.3531 90.6595 84.5568 79.2202 96.5951 51.1510 57.3575 58.1255 59.8341 95.8151 82.8493 65.5103 89.5822 99.6874 95.5243 84.5798 94.8040 57.4110 73.4717 54.5961 78.1594 99.7419 95.5839 67.1404 58.1216 94.4901 56.3679 61.1848 74.0492 91.6894 67.8450 80.0691 55.1860 82.2985 57.1549 82.1788 97.2404 74.3354 80.3498 77.4717 90.7838 83.7799 60.1565 69.4781 78.7974 63.2917 65.4941 67.5822 87.9557 63.1289 62.3104 94.9936 54.2756 70.0754 95.0130 67.7527 87.6007 52.8911 90.9698 70.0772 61.6541 66.5833 52.8302 80.3913 54.4871 52.8131 54.8970 77.3278 89.3630 72.5458 87.1111 97.8071 68.3625 50.4987 61.0646 95.9497 59.6621 74.1439 67.7919 98.5115 72.6058 98.2891 81.1950 97.2313 50.1491 72.8012 99.2500 84.2681 53.7348 86.7432 63.7273 90.7379 71.5178 82.9228 51.2806 70.7717 90.0404 58.0104 85.0303 RH_(:,:,2) = 56.6951 98.4780 88.3247 84.7453 97.7123 59.0404 97.6004 66.4359 86.1503 89.4997 96.6962 99.4605 61.0868 57.4627 87.0808 60.1727 63.1721 85.5627 62.9059 66.2662 69.5142 55.8568 53.7216 92.0934 82.9532 53.3873 56.8598 70.4513 92.1630 68.2080 84.4651 84.4958 61.1023 60.7748 55.4029 91.5869 89.2143 84.1657 50.7386 82.5873 51.5665 94.9017 97.4558 81.3093 59.9058 83.0656 57.5397 75.5881 64.1115 59.4591 79.3144 87.2706 79.5560 82.8938 96.4376 89.2934 78.4395 55.5257 51.4124 55.0121 87.1905 84.2470 80.8490 95.6476 98.5553 75.6099 64.6956 50.5216 74.5220 69.4426 89.4278 81.7719 96.6996 72.9858 59.1341 52.9581 62.0306 94.6157 56.0292 80.5082 78.3852 81.2894 71.0934 51.5648 62.4164 53.2589 52.9741 78.2768 87.2956 90.8467 52.5164 57.8192 90.1806 99.6002 94.3777 97.9295 52.9700 51.1391 61.8328 81.0458 88.1675 59.5932 74.0944 83.6468 83.1799 51.1294 85.3137 80.6391 90.9771 76.3925 56.5633 73.2430 66.6102 82.9369 75.2057 55.4768 91.2837 57.9982 79.4566 77.8842 82.1511 79.0554 54.0523 75.4427 73.9337 57.9603 69.1745 71.3645 82.5514 88.5932 99.0980 52.7623 77.9465 87.9143 69.9231 66.6446 67.4130 91.0854 68.4407 79.7608 81.4794 87.3356 59.4294 61.1187 83.7304 83.5594 57.2195 52.3474 69.8230 93.5025 95.2865 60.1983 61.0480 86.6514 75.7878 99.9447 51.0120 74.7800 86.1853 94.4746 80.2841 75.1778 90.4256 76.7178 80.9791 65.3984 58.4431 53.9863 57.5800 88.4979 95.8619 95.4774 56.1253 76.6519 84.1279 96.9981 60.8861 57.3860 88.7620 76.8883 64.4044 64.5109 98.9701 69.0928 99.5107 98.5748 63.2670 97.3899 69.4102 52.5865 74.6513 94.6158 53.4698 95.7772 69.9644 93.7236 82.1246 50.3608 68.5650 85.3054 72.6753 51.4630 77.4744 67.5419 79.7895 52.0508 56.0002 88.8847 67.8868 78.2046 56.5940 98.4157 72.0232 73.8555 63.0331 56.4532 56.1722 63.2447 85.0828 54.8800 69.4781 66.1831 50.2682 78.0862 53.6298 91.4698 91.6425 89.6644 91.2032 63.3307 52.5639 68.8667 85.9701 97.3154 60.0270 92.9344 81.6987 72.4260 64.2058 82.3816 94.3674 84.6721 57.3527 79.4953 80.2541 66.5173 86.1048 96.1064 95.3057 95.0258 91.9677 67.4051 77.6651 57.4514 55.1769 86.4168 70.8190 83.7904 91.7319 98.0166 90.7887 56.3621 72.7692 99.0662 93.5224 57.1471 65.5658 68.1524 79.4049 99.7102 63.8918 58.0472 92.2798 89.7735 70.8049 93.1728 77.5961 92.3513 53.5510 67.5922 87.3880 70.6707 99.3028 94.8155 95.1096 89.3278 73.1906 55.6765 53.3104 59.2489 91.4268 53.0874 50.4527 69.1141 70.6087 73.3218 77.3373 67.3494 76.8841 92.0594 80.3738 59.1070 82.2099 81.0102 89.4119 53.1488 63.6939 86.0357 55.2143 93.3677 98.0482 91.8306 89.3110 59.2079 84.5030 64.7075 63.1917 65.9057 88.4122 87.1085 59.7936 99.0853 88.1566 68.8991 69.3430 88.8708 85.5750 61.1117 87.8662 76.7476 60.0245 74.4009 66.8322 60.1892 51.1177 81.5428 90.7108 89.7866 75.0558 54.6345 86.2026 61.7648 90.8792 71.2832 93.1553 95.7763 52.0512 65.0272 87.4132 57.3407 60.5849 87.1444 56.8857 59.4025 76.3049 81.1140 98.7415 74.6055 65.8889 96.1799 56.9989 59.1632 92.0604 73.8196 57.7021 82.2982 61.2442 87.0855 82.1267 92.2494 90.0321 54.8911 80.8815 54.5750 64.2758 75.6226 50.9769 56.0472 84.8180 87.1120 84.9194 64.8061 52.5324 73.0841 86.0655 72.1670 97.4348 80.2096 54.8632 79.1498 80.5690 53.2790 71.1485 75.8303 59.4903 97.2964 80.0323 69.0426 50.6990 93.6095 RH_(:,:,3) = 84.6973 68.3791 53.9026 59.4599 91.9205 73.5561 82.7187 91.9053 56.5757 85.3003 99.6813 85.7107 83.0117 79.2496 86.1775 52.8485 61.3920 92.0351 93.0774 88.4747 92.7721 62.9444 88.7897 81.7719 86.8552 98.1852 78.0147 81.2769 63.0540 98.2431 76.7471 75.3798 96.8209 64.9507 87.7763 70.2795 51.6734 63.5959 67.7987 76.4218 71.5021 97.4576 91.2950 53.7591 98.7734 53.1026 61.3238 56.4085 94.9308 71.4104 50.6639 53.6209 73.2359 90.3171 86.0231 84.4439 76.0686 75.3407 75.4280 72.3323 57.4734 97.3059 67.1458 90.9837 69.9512 57.3873 52.2770 96.7015 74.1191 57.7704 71.8395 66.7100 95.2268 71.3148 77.1516 66.1395 61.3238 57.7055 71.2873 56.0123 68.8757 65.2833 83.0146 66.3450 79.1978 94.1059 98.3606 79.3494 63.3723 50.0235 76.1580 90.3267 55.9386 89.6581 96.0343 78.2155 51.2557 61.1771 96.8369 55.6487 66.8467 51.4627 58.9711 76.3401 73.9878 80.5199 85.2555 59.9923 71.7274 94.6885 71.7015 65.1897 92.3378 98.1257 94.2765 59.4044 61.0848 58.0555 78.3345 57.7132 82.8906 93.4542 81.4592 93.7526 83.0407 54.1730 84.8085 87.2253 90.3935 65.4056 53.7907 77.1497 80.5502 62.6469 89.2991 88.7006 71.3535 72.0330 81.9614 88.5629 68.0427 84.1180 74.7020 59.1524 63.0027 89.0902 96.5270 66.4599 76.9052 99.0690 77.4165 50.8719 56.8514 67.1866 61.4768 66.0223 98.2311 96.8439 66.3020 70.6515 86.1113 51.5382 66.9494 98.3482 57.3235 63.3255 61.2185 88.9406 73.4328 64.3856 56.7541 89.3963 99.0894 95.5960 68.3269 75.3295 94.9262 51.3218 50.3418 76.5180 53.9272 62.8415 57.9931 72.5088 88.0500 59.8578 78.7607 82.1858 57.4315 86.6735 71.2322 81.2364 58.7298 64.4175 74.6146 66.5158 89.2721 67.9529 78.5743 89.5544 66.2127 93.2575 70.2375 65.8891 63.1510 63.6624 80.2223 57.0264 90.1143 75.1196 67.6659 87.2973 55.9933 99.4845 69.4473 63.2043 79.9117 62.7034 77.2229 76.4331 78.4075 98.1110 55.8102 84.5466 88.5536 82.1664 57.8394 96.1558 86.1786 89.9566 55.8444 92.9577 64.6937 58.6872 83.1935 76.1739 61.6063 86.7477 70.3237 57.0877 86.8589 89.5805 78.3296 88.9209 80.8020 59.9292 53.9782 55.0844 95.4442 89.6662 92.5144 70.6920 91.9920 95.1417 91.2064 92.7347 63.8969 89.7969 59.3338 55.0266 81.7218 96.7166 59.0788 73.3559 93.1481 60.9483 56.6130 74.1393 77.9722 82.8131 50.0562 81.6878 64.6889 80.0585 95.7853 82.6849 51.5658 99.1975 68.1539 58.6878 63.7504 97.5272 53.1081 89.2464 53.6311 58.3109 76.1106 64.1955 82.6558 82.2356 68.7752 81.7023 62.7943 79.4181 83.2257 51.7875 82.1602 57.2607 55.4111 97.7529 77.4205 82.5465 94.8846 81.1491 53.6035 76.5379 86.2794 58.0774 51.0047 83.6963 92.6222 94.9579 81.2503 60.7299 67.6720 95.7822 54.5181 91.7472 60.6412 77.2823 96.1918 71.9604 50.7713 98.9711 90.0044 99.0273 77.0433 92.1843 96.8764 66.8982 91.1656 60.3811 79.7269 85.7642 83.7518 87.7226 94.2814 75.7031 67.2017 50.7374 53.9782 92.8214 77.1837 60.6912 50.4684 53.2087 78.0080 96.7973 57.4266 63.3790 78.2923 87.7276 60.5048 97.2706 91.7797 85.3303 92.2281 70.4848 64.6316 76.2100 80.9539 55.8919 85.9053 85.4722 71.9939 99.1161 54.9727 91.3479 65.3004 91.1892 65.5940 93.5508 68.5772 70.8278 59.3256 61.5449 93.6872 73.9264 89.2442 78.9498 93.3517 93.4050 58.7622 51.5146 50.4427 59.8756 53.2683 68.6011 68.6408 70.9885 79.1355 86.4844 68.5417 51.2142 77.3312 84.8428 99.4255 93.8101 50.5027 92.3297
RH_excess_days = squeeze(sum(RH_ > 99,1))/24
RH_excess_days = 4×3
0 0.0417 0.0833 0.1250 0.1250 0.2083 0.1250 0.0833 0.0417 0.1667 0.1250 0
  2 Comments
DGM
DGM on 28 Oct 2021
It's a bit slower, but it's possible. I don't know that the speed difference matters here though.
RH_ = 50 + 50*rand(100,4,3); % example data
% loop method
s = size(RH_);
RH_excess_days1 = zeros(s(2:3));
for col = 1:s(2)
for page = 1:s(3)
RH_excess_days1(col,page) = sum(RH_(:,col,page) > 99)/24;
end
end
% vectorized method
RH_excess_days = squeeze(sum(RH_ > 99,1))/24;
% two results should match
immse(RH_excess_days1,RH_excess_days)
ans = 0

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!