Adding 2D array to 3D array within loop
조회 수: 6 (최근 30일)
이전 댓글 표시
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
채택된 답변
Torsten
2023년 8월 5일
편집: Torsten
2023년 8월 5일
totalarray = rand(365,721,1440);
newdata = rand(721,1440);
squeeze(totalarray(1,:,:))
newdata
for i = 1:365
totalarray(i,:,:)=squeeze(totalarray(i,:,:))+newdata(:,:);
end
squeeze(totalarray(1,:,:))-newdata
댓글 수: 6
Bruno Luong
2023년 8월 6일
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)
M(1,:,:)=v; % But this works
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!