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!