Adding 2D array to 3D array within loop
6 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
John Cruce
el 5 de Ag. de 2023
Comentada: Bruno Luong
el 6 de Ag. de 2023
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 comentario
Respuesta aceptada
Torsten
el 5 de Ag. de 2023
Editada: Torsten
el 5 de Ag. de 2023
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 comentarios
Bruno Luong
el 6 de Ag. de 2023
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
Bruno Luong
el 6 de Ag. de 2023
I think I'll ask in a separete question to get more visible to others
Más respuestas (0)
Ver también
Categorías
Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!