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!