Fixing incompatible array sizes in population dynamics code
Show older comments
For the code below I am getting the error code:
Arrays have incompatible sizes for this operation.
Error in LastName_Assignment5 (line 79)
nt(s, :) = nt(s, :) .* M'; % Element-wise multiplication (note the transpose on M)
% Simulation loop
for t = 1:time_steps
nt = L * nt;
for site = 1:site_numbers % Apply Lefkovitch matrix to each site separately
nt(:, site) = L * nt(:, site); % Apply to each subpopulation
end
% Incorporate Ricker model
nt = nt .* exp(beta * (1 - sum(nt, 1) ./ K));
for s = 1:life_stages
nt(s, :) = nt(s,:) * M; % Migration applied to each life stage
end
record_individuals(t, :, :) = nt;
end
1 Comment
Steven Lord
on 1 Apr 2025
Without seeing the sizes of the matrices involved in that operation, about the best I think we can do is direct you to the description of what it means for matrices to be compatibly sized in MATLAB.
Note that the code in the error message (which uses element-wise multiplication and the conjugate transpose) is not the same as the code you posted after that (where what I believe to be the corresponding line uses matrix multiplication and no transpose, with a different comment.) Which line of code actually appears on line 79 of LastName_Assignment5?
Answers (0)
Categories
Find more on Operators and Elementary Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!