Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is 1-by-2.

40 views (last 30 days)
I'm writing a stochastic simulation of predator-prey dynamics. To simulate it properly I had to put both the calculation of the number of prey and the number of predator individuals in one function, but it created issues with assignment. I am confused as to why, as I assigned an i-th element of a vector to a specific value of population, like I did in my previous codes and it has been working properly. Any ideas as to why it is happening? The only relevant solution I've been able to find is allocating the size of the array and the vectors inside it and filling it with zeros, which I already done and it is not helping. The code is as below and the errors are displayed for like 107 and 108, which allocate V or C to Results(1,i) or Results(2,i).
%C- predator individuals
% V- prey individuals
% um- maximum growth rate of predator
% VV- threshold prey concentration (predator growth rate = 0 )
% hh- constant
% dm- max mortality rate ?
% hd- constant
% r- prey specific growth rate
% h- half saturation constant
% IM- maximum predator ingestion rate
%delta_t - timestep
%constants
um = 1.4275; VV = 12.5; hh = 49.7; dm = 0.48; hd = 49.7; %C parameters
r = 0.4; h = 261; IM = 28.5; %V parameters
delta_t=0.2;
%arrays of values
params_C=[um VV hh dm hd];
params_V=[r h IM]; %lists containing population dynamics parameters
%access to functions via handles
fh=localfunctions; %store all functions
PopulationVariables = fh{5}(2,50,delta_t); %stored in vector not an array, can change if needed
C = PopulationVariables(1,1);
V = PopulationVariables(1,2);
C_vector = fh{6}(C,V,delta_t,params_C);
V_vector = fh{6}(C,V,delta_t,params_V);
Results_array = [C_vector; V_vector];
Birth_Rate_C = fh{1}(C_vector,V_vector,um,VV,hh,dm,hd);
Birth_Rate_V = fh{2}(V_vector,r);
Death_Rate_C = fh{3}(C_vector,V_vector,dm,hd);
Death_Rate_V = fh{4}(C_vector,V_vector,r,h,IM);
%plot
figure
left_color = [0 0 1];
right_color = [1 0 0];
set(figure,'defaultAxesColorOrder',[left_color;right_color]);
yyaxis right
plot(V_vector,'-r.');
ylabel('Number of prey individuals');
yyaxis left
plot(C_vector,'-b.');
ylabel('Number of predator individuals');
legend('Prey','Predator');
hold on; grid on;
hold off;
%birth rate of predator
function [brc] = BirthRate_C(C,V,um,VV,hh,dm,hd)
brc=((C*um.*(V-VV))./(hh+V-VV))+((-dm.*V)/(hd+V)+dm).*C;
end
%birth rate of prey
function [brv] = BirthRate_V(V,r)
brv=r*V;
end
%death rate of predator
function [drc] = DeathRate_C(C,V,dm,hd)
drc=((-dm*V)./(hd+V)+dm).*C;
end
%death rate of prey
function [drv] = DeathRate_V(C,V,r,h,IM)
drv=(r.*V.^2)./h +(IM.*C.*V)./(h+V);
end
function [Population_variables] = timestep(C,V,~,~)
um = 1.4275; VV = 12.5; hh = 49.7; dm = 0.48;
hd = 9.35; delta_t = 0.2;
r = 0.4; h = 261; IM=28.5;
lambda_V = BirthRate_V(V,r)*delta_t;
pd_V = poissrnd(lambda_V);
V = V + pd_V;
V_survive = exp((-DeathRate_V(C,V,r,h,IM)*delta_t)/V);
V = binornd(V,V_survive);
lambda_C = BirthRate_C(C,V,um,VV,hh,dm,hd)*delta_t;
pd_C = poissrnd(lambda_C);
C = C + pd_C;
C_survive = exp((-DeathRate_C(C,V,dm,hd)*delta_t)/C);
C = binornd(C,C_survive);
Population_variables=[C,V];
end
function [Results] = simulation(C,V,delta_t,~)
um = 1.4275; VV = 12.5; hh = 49.7; dm = 0.48; hd = 49.7;
r = 0.4; h = 261; IM = 28.5;
Results = zeros(2,100);
params_V=[r h IM];
params_C=[um VV hh dm hd];
for i=1:1:100
V = timestep(C,V,delta_t,params_V);
C = timestep(C,V,delta_t,params_C);
Results(1,i) = V;
Results(2,i) = C;
end
end

Accepted Answer

Adam
Adam on 11 Jul 2018
Your timestep function returns this:
Population_variables=[C,V];
Yet you call it twice as:
V = timestep(C,V,delta_t,params_V);
C = timestep(C,V,delta_t,params_C);
and attempt to assign the results of each of these to something which is clearly scalar (Results(1,i)).
So the error message gives exactly what is wrong. Your returned variable has concatenated both C and V and hence is of size [1,2] and cannot be assigned to a scalar.
I'm not sure exactly what your purpose is and exactly what you want to assign to what so I can't really tell how to correct it.
  4 Comments
aqsa
aqsa on 31 Jul 2019
Unable to perform assignment because the size of the left side is 1-by-3377160 and the size of the right side is 1-by-3060288.

Sign in to comment.

More Answers (2)

mani kandan
mani kandan on 25 Jul 2019
A=[0 0.004 0.008 0.012 0.016 0.020 0.024];
B=[0 0.005334 0.010666 0.016 0.021334 0.026668 0.032];
C=[0 0.004 0.008 0.012 0.016 0.020 0.024];
[m n]=size(A)
[m n]=size(B)
[m n]=size(C)
for i=1:length(A)
for j=1:length(B)
for k=1:length(C)
K(i,j,k)=A(1:n).*B(1:n).*C(1:n)
end
end
end
but i got an error like this while attempting matrix multiplication
Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is 1-by-7.
Error in main (line 10)
K(i,j,k)=A(1:n).*B(1:n).*C(1:n)
anyone please clarify this. thanks by advance.

Walter Roberson
Walter Roberson on 25 Jul 2019
A(i) * B(j) * C(k)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!