
Help fixing error code, please!
조회 수: 1 (최근 30일)
이전 댓글 표시
Hello! Any and all help would be greatly appreciated!
I'm trying to run my code (below), but keep getting the following error code:
Indexin position 1 is invalid. Array indices must be positive integers or logical values.
Error in M_Model (line 56)
RHS(:,pp)=((1-alpha)*Ft(xp(:,pp),tt+1))+
(alpha*(1-Y)*Ft(xpp(:,pp),tt+1))+(alpha*Y*w*Ft(xppp(:,pp),tt+1))+(alpha*Y*(1-w)*Ft(xpppp(:,pp),tt+1));
So here is the code to my chop-it function, with the code I'm trying to run below that. Thank you VERY much in advance for any help/insights on how to fix this!!
%CHOPIT
function [state2]=chopit(state1,bottom,top)
%CHOPIT truncates state values at a low of xcrit and a high of Cap
%input is vector of state values, followed by single values for xcrit and
%cap
%outputs a vector of truncated state values
state2=(state1<=bottom).*bottom+(state1>bottom).*(state1<=top).*state1+(state1>top).*top;
end
%CODE
%specify constants
term = 250; % terminal time
Cap = 10; % cap or max state
xcrit = 3 ; % critical value of x [range 1 to 10]
nchoice = 2 ; % number of choices (varied cost/benefit of fight)
%initialize arrays and vectors used in the program
RHS = zeros(Cap,nchoice); % at each time step, expected fitness of each choice for individual of a given state
opt = zeros(Cap,term); % optimal choice at each state at each time
Ft = zeros(Cap,term); % an array with max expected fitness at each state at each time
%x=1-10 (rows) at each time t=1-20 (columns)
%at each time step, for each state, this will be the max fitness over the
%two choices, which we will calculate below so for now, fill this array
%with zeros
x = linspace(1,Cap,Cap)'; %column vector of state values
%initialize x' and x" arrays with a row for each state and column for each
%patch
xp = zeros(Cap,nchoice); % xprime for each state value, each patch
xpp = zeros(Cap,nchoice); % x double prime for each state value, each patch
%Specify patch parameter values
alpha = [0.25 0.25]; % probability of encountering another male
beta = [0.95 0.95]; % probability of surviving to next time-step
Y = [0.5 0.5]; % probablity of fighting
lambda = [0.5 0.5]; % rate of decay in RHP if no fight
w = [(1/(1+(exp(x)))) (1/(1+(exp(x))))]; % probability of winning a fight
c = [3 3]; % cost of losing a fight
v = [1 1]; % gain of winning a fight
%Specify values of xprime, x double prime, etc
for pp = 1:nchoice %loop through the choices
xp(:,pp) = chopit(x-lambda(pp),xcrit,Cap);
xpp(:,pp) = chopit(x-lambda(pp),xcrit,Cap);
xppp(:,pp) = chopit(x+v(pp),xcrit,Cap); %#ok<AGROW>
xppp(:,pp) = chopit(x-c(pp),xcrit,Cap);
end
%specify terminal fitness
Ft((1:xcrit),end)=0; % fitness=0 at or below xcrit
Ft((4:7),end) = 0.7; % fitness = 0.7 for sneak
Ft((8:Cap),end)=1 ; % fitness=1 for territorial
%make a loop that begins at one step before terminal time and goes back to t=1
for tt= term-1:-1:1 % loop backwards through time
for pp=1:nchoice; % loop through each patch
%dynamic programming equation
RHS(:,pp)=((1-alpha(pp))*Ft(xp(:,pp),tt+1))+ (alpha(pp)*(1-Y(pp))*Ft(xpp(:,pp),tt+1))+(alpha(pp)*Y(pp)*w(pp)*Ft(xppp(:,pp),tt+1))+(alpha(pp)*Y(pp)*(1-w(pp))*Ft(xpppp(:,pp),tt+1));
RHS(1:xcrit,pp)=0;
end
%now find maximum RHS (i.e. max fitness & associated choice)
%the fit and opt arrays will hold all model output
%want max function to return a column vector with the max value of each
%row of RHS. Recall RHS consists of expected fitness of each choice
%(columns) for each state value (rows) & want each row max
[Ft(:,tt),opt(:,tt)]= max(RHS,[],2);
end
댓글 수: 13
Walter Roberson
2020년 4월 15일
Ft(xp(:,pp),tt+1)) asks to recall xp(:,pp) and use it as the first index into the array Ft. Are you certain that xp(:,pp) will be strictly positive integers?
답변 (1개)
Image Analyst
2020년 4월 15일
Probably one of the most Frequently Asked of the Frequently Asked Questions. So See the FAQ for a thorough discussion:
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!