Adding 2D array to 3D array within loop

I have a 3d array of zeros size (365,721,1440). I'm iterating through a for loop to add from array (721,1440) to the 3d array for each 1:365. Effectively, I'm wanting to do something like this:
totalarray(i,:,:)=totalarray(i,:,:)+newdata(:,:);
But obviously that throws me an error (Array dimensions must match for binary array op). Any idea how to add this new data I'm creating with each iteration to the total array?

1 Comment

The robust approach avoiding SQUEEZE is to use PERMUTE, e.g. inside the loop:
totalarray(i,:,:) = totalarray(i,:,:) + permute(newdata(:,:),[3,1,2]);

Sign in to comment.

 Accepted Answer

totalarray = rand(365,721,1440);
newdata = rand(721,1440);
squeeze(totalarray(1,:,:))
ans = 721×1440
0.9180 0.7403 0.9442 0.6863 0.8394 0.5977 0.4434 0.9624 0.6240 0.7789 0.5832 0.5035 0.7979 0.4041 0.5983 0.3894 0.2274 0.2399 0.4560 0.0206 0.4144 0.6171 0.3203 0.0168 0.8789 0.5111 0.8552 0.4301 0.6461 0.6321 0.3525 0.9185 0.0988 0.5666 0.2365 0.2108 0.9625 0.1204 0.2400 0.8816 0.3561 0.6151 0.7968 0.7438 0.2618 0.6526 0.0194 0.4032 0.9423 0.4526 0.7445 0.3585 0.4404 0.9950 0.8167 0.2546 0.8617 0.3610 0.0185 0.5758 0.9099 0.9810 0.0050 0.4039 0.9070 0.7620 0.4528 0.9870 0.2616 0.4812 0.3090 0.8578 0.7012 0.6637 0.2347 0.4635 0.2407 0.8730 0.2824 0.5364 0.9783 0.8739 0.9082 0.3018 0.4118 0.5963 0.6822 0.5497 0.7222 0.2409 0.3404 0.3833 0.1079 0.3026 0.8795 0.7419 0.3381 0.9935 0.2534 0.3623 0.9849 0.8293 0.7670 0.3845 0.8292 0.2274 0.6246 0.7675 0.0875 0.8846 0.7229 0.9455 0.4574 0.2916 0.3537 0.2259 0.7163 0.4403 0.5925 0.4643 0.6796 0.7045 0.9304 0.4964 0.6210 0.7121 0.4238 0.1424 0.6906 0.6048 0.6481 0.9398 0.4049 0.9680 0.3860 0.8715 0.9419 0.8070 0.9225 0.9702 0.0564 0.5348 0.4018 0.1418 0.5718 0.6908 0.2867 0.7117 0.9550 0.3775 0.1733 0.4875 0.7058 0.4508 0.8729 0.5887 0.2104 0.4657 0.5697 0.8856 0.2466 0.1478 0.1918 0.4356 0.3903 0.4003 0.8102 0.7488 0.2213 0.8581 0.2131 0.1030 0.3792 0.1844 0.2401 0.7674 0.7793 0.0751 0.0511 0.0766 0.5268 0.5218 0.8256 0.8983 0.2225 0.4608 0.2592 0.3078 0.1806 0.0341 0.0125 0.7160 0.3720 0.0708 0.5327 0.7104 0.2295 0.4033 0.0143 0.1474 0.4705 0.9806 0.7691 0.7418 0.9536 0.7874 0.3542 0.1520 0.2379 0.2383 0.2001 0.0328 0.2337 0.3656 0.3641 0.6681 0.8772 0.4709 0.3654 0.2162 0.0318 0.2055 0.4044 0.9056 0.8325 0.3972 0.2610 0.6769 0.2282 0.9662 0.0612 0.8916 0.2086 0.3956 0.9327 0.9214 0.4151 0.4498 0.5307 0.4012 0.0485 0.0869 0.7273 0.9606 0.4810 0.1507 0.4744 0.7508 0.2955 0.3601 0.1234 0.0952 0.2242 0.0694 0.2134 0.0094 0.3020 0.3279 0.1929 0.0123 0.9108 0.1594 0.1994 0.0373 0.2910 0.1065 0.9818 0.2966 0.5297 0.6438 0.6840 0.3363 0.2713 0.3576 0.7629 0.2958 0.5539 0.6363 0.1863 0.6901 0.3004 0.1815 0.4398 0.0904 0.5276 0.3253 0.0399 0.4353 0.2279 0.3832 0.7812 0.5513 0.7459 0.2446 0.2601 0.1010 0.7443 0.7047 0.1154 0.9106
newdata
newdata = 721×1440
0.3055 0.9525 0.1939 0.5656 0.3264 0.4077 0.2867 0.1277 0.5417 0.2422 0.8504 0.9468 0.7681 0.7222 0.2938 0.2369 0.3955 0.9816 0.1759 0.6876 0.7899 0.3958 0.7094 0.3803 0.8130 0.1588 0.5729 0.1190 0.7107 0.0030 0.7729 0.7734 0.5378 0.1872 0.7650 0.7394 0.6198 0.8529 0.5647 0.4154 0.4782 0.8490 0.6765 0.5201 0.1543 0.8161 0.2849 0.8946 0.4749 0.2000 0.0452 0.4117 0.3485 0.5924 0.4287 0.2122 0.9401 0.2599 0.8172 0.8658 0.6798 0.8855 0.3265 0.2858 0.0643 0.3721 0.2426 0.4622 0.0092 0.4493 0.0270 0.8284 0.1620 0.8471 0.5345 0.2865 0.4786 0.2376 0.7596 0.4692 0.0854 0.4451 0.3848 0.2226 0.0683 0.8597 0.6666 0.9165 0.9614 0.1105 0.5993 0.9617 0.5919 0.0541 0.0755 0.3134 0.9879 0.8500 0.7821 0.4782 0.9411 0.2385 0.1511 0.4519 0.4333 0.8353 0.4779 0.8709 0.0190 0.0670 0.6135 0.0584 0.5449 0.1919 0.7425 0.1847 0.2467 0.8357 0.1343 0.6207 0.4026 0.3645 0.8392 0.0831 0.9765 0.8641 0.0740 0.2607 0.7417 0.9087 0.8713 0.7614 0.0320 0.9100 0.6449 0.5718 0.7865 0.9480 0.9700 0.2585 0.2334 0.9652 0.3249 0.8944 0.0340 0.0967 0.4310 0.8865 0.3138 0.6828 0.4058 0.8199 0.3658 0.2186 0.7358 0.8380 0.4910 0.7623 0.6318 0.1847 0.2507 0.2270 0.7121 0.1699 0.0554 0.1013 0.7941 0.2391 0.1909 0.1481 0.8970 0.7278 0.3771 0.1150 0.2383 0.5073 0.8240 0.8893 0.1994 0.6386 0.2944 0.3914 0.3253 0.4870 0.8307 0.2496 0.7897 0.4705 0.9255 0.9174 0.0917 0.9445 0.4681 0.3413 0.5292 0.8798 0.9710 0.8278 0.6057 0.9887 0.2685 0.6637 0.9822 0.5773 0.8273 0.1851 0.6225 0.5118 0.0222 0.5489 0.7444 0.7727 0.0729 0.2738 0.7131 0.0560 0.7964 0.8715 0.8032 0.8399 0.6415 0.7498 0.6395 0.3618 0.9688 0.5533 0.1456 0.1894 0.8611 0.4335 0.1508 0.5761 0.7659 0.0973 0.2885 0.7930 0.3601 0.5473 0.2266 0.0614 0.9997 0.9776 0.1400 0.0641 0.8306 0.5158 0.6687 0.4564 0.2149 0.5413 0.9368 0.5342 0.5678 0.6941 0.1299 0.2821 0.2538 0.5806 0.2616 0.7413 0.1544 0.8241 0.6549 0.8108 0.6849 0.6357 0.1402 0.3796 0.5517 0.7123 0.9054 0.4918 0.4470 0.6017 0.7618 0.5326 0.0907 0.8633 0.7658 0.2938 0.1662 0.6496 0.2920 0.4888 0.4386 0.4805 0.6625 0.9880 0.9101 0.7173 0.6183 0.6784 0.8134 0.0029 0.5616 0.3775 0.9112 0.7606 0.1493 0.3026
for i = 1:365
totalarray(i,:,:)=squeeze(totalarray(i,:,:))+newdata(:,:);
end
squeeze(totalarray(1,:,:))-newdata
ans = 721×1440
0.9180 0.7403 0.9442 0.6863 0.8394 0.5977 0.4434 0.9624 0.6240 0.7789 0.5832 0.5035 0.7979 0.4041 0.5983 0.3894 0.2274 0.2399 0.4560 0.0206 0.4144 0.6171 0.3203 0.0168 0.8789 0.5111 0.8552 0.4301 0.6461 0.6321 0.3525 0.9185 0.0988 0.5666 0.2365 0.2108 0.9625 0.1204 0.2400 0.8816 0.3561 0.6151 0.7968 0.7438 0.2618 0.6526 0.0194 0.4032 0.9423 0.4526 0.7445 0.3585 0.4404 0.9950 0.8167 0.2546 0.8617 0.3610 0.0185 0.5758 0.9099 0.9810 0.0050 0.4039 0.9070 0.7620 0.4528 0.9870 0.2616 0.4812 0.3090 0.8578 0.7012 0.6637 0.2347 0.4635 0.2407 0.8730 0.2824 0.5364 0.9783 0.8739 0.9082 0.3018 0.4118 0.5963 0.6822 0.5497 0.7222 0.2409 0.3404 0.3833 0.1079 0.3026 0.8795 0.7419 0.3381 0.9935 0.2534 0.3623 0.9849 0.8293 0.7670 0.3845 0.8292 0.2274 0.6246 0.7675 0.0875 0.8846 0.7229 0.9455 0.4574 0.2916 0.3537 0.2259 0.7163 0.4403 0.5925 0.4643 0.6796 0.7045 0.9304 0.4964 0.6210 0.7121 0.4238 0.1424 0.6906 0.6048 0.6481 0.9398 0.4049 0.9680 0.3860 0.8715 0.9419 0.8070 0.9225 0.9702 0.0564 0.5348 0.4018 0.1418 0.5718 0.6908 0.2867 0.7117 0.9550 0.3775 0.1733 0.4875 0.7058 0.4508 0.8729 0.5887 0.2104 0.4657 0.5697 0.8856 0.2466 0.1478 0.1918 0.4356 0.3903 0.4003 0.8102 0.7488 0.2213 0.8581 0.2131 0.1030 0.3792 0.1844 0.2401 0.7674 0.7793 0.0751 0.0511 0.0766 0.5268 0.5218 0.8256 0.8983 0.2225 0.4608 0.2592 0.3078 0.1806 0.0341 0.0125 0.7160 0.3720 0.0708 0.5327 0.7104 0.2295 0.4033 0.0143 0.1474 0.4705 0.9806 0.7691 0.7418 0.9536 0.7874 0.3542 0.1520 0.2379 0.2383 0.2001 0.0328 0.2337 0.3656 0.3641 0.6681 0.8772 0.4709 0.3654 0.2162 0.0318 0.2055 0.4044 0.9056 0.8325 0.3972 0.2610 0.6769 0.2282 0.9662 0.0612 0.8916 0.2086 0.3956 0.9327 0.9214 0.4151 0.4498 0.5307 0.4012 0.0485 0.0869 0.7273 0.9606 0.4810 0.1507 0.4744 0.7508 0.2955 0.3601 0.1234 0.0952 0.2242 0.0694 0.2134 0.0094 0.3020 0.3279 0.1929 0.0123 0.9108 0.1594 0.1994 0.0373 0.2910 0.1065 0.9818 0.2966 0.5297 0.6438 0.6840 0.3363 0.2713 0.3576 0.7629 0.2958 0.5539 0.6363 0.1863 0.6901 0.3004 0.1815 0.4398 0.0904 0.5276 0.3253 0.0399 0.4353 0.2279 0.3832 0.7812 0.5513 0.7459 0.2446 0.2601 0.1010 0.7443 0.7047 0.1154 0.9106

6 Comments

I'm confused as a MATLAB beginner (!)
I though such assigment
totalarray(i,:,:)=squeeze(totalarray(i,:,:))+newdata(:,:);
would throw an error
"Unable to perform assignment because the size of the left side is 1-by-721-by-1440 and the size of the right side is 721-by-1440."
but obviously it's not.
Can someone points me to the official document that explain such assigment is allowed and how?
Assigning an 1d row vector to the ith row of a matrix is the same thing:
v = rand(1,5);
M = zeros(3,5);
M(3,:) = v
M = 3×5
0 0 0 0 0 0 0 0 0 0 0.8197 0.2924 0.0199 0.8188 0.6605
Can you modify my (wrong) allowedassgn below so that it reflects correctly when the assignment
lhs = rhs
is allowed?
allowedassgn = @(lhs,rhs) isequal(size(lhs), size(rhs)) || ...
(isvector(lhs) && isvector(rhs) && length(lhs)==length(rhs)) || ...
(isempty(rhs) && isempty(lhs));
v = rand(1,5);
M = zeros(3,5);
allowedassgn(M(3,:), v)
ans = logical
1
M(3,:) = v; % so this is allowed
v = rand(5,1);
M = zeros(3,5);
allowedassgn(M(3,:), v) % idem
ans = logical
1
M(3,:) = v
M = 3×5
0 0 0 0 0 0 0 0 0 0 0.1687 0.1125 0.1724 0.9949 0.6670
v = rand(4,5);
M = zeros(3,4,5);
allowedassgn(M(3,:,:), v)
ans = logical
0
M(3,:,:) = v % but this works !!!
M =
M(:,:,1) = 0 0 0 0 0 0 0 0 0.6594 0.8731 0.3168 0.0578 M(:,:,2) = 0 0 0 0 0 0 0 0 0.2092 0.0008 0.4694 0.9609 M(:,:,3) = 0 0 0 0 0 0 0 0 0.1171 0.4693 0.2653 0.1701 M(:,:,4) = 0 0 0 0 0 0 0 0 0.3111 0.4261 0.2979 0.8661 M(:,:,5) = 0 0 0 0 0 0 0 0 0.4661 0.3910 0.2764 0.9486
Maybe
allowedassgn = @(lhs,rhs) isequal(size(squeeze(lhs)), size(rhs)) || ...
(isvector(lhs) && isvector(rhs) && length(lhs)==length(rhs)) || ...
(isempty(rhs) && isempty(lhs));
?
But
allowedassgn = @(lhs,rhs) isequal(size(squeeze(lhs)), size(rhs)) || ...
(isvector(lhs) && isvector(rhs) && length(lhs)==length(rhs)) || ...
(isempty(rhs) && isempty(lhs));
fails to detect
M = zeros(3,4,5);
v=rand(1,4,5);
allowedassgn(M(1,:,:),v)
ans = logical
0
M(1,:,:)=v; % But this works
I think I'll ask in a separete question to get more visible to others

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2023a

Community Treasure Hunt

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

Start Hunting!