function [reward,newState] = gridworldStep(world,state,action)

reward = getReward(world,state,action); %look up reward for this state-action pair

%determine new state
newState = state;
dim = ceil(action/2); %which dimension the action is on
dir = 1 - mod(action,2)*2; %-1 for down; +1 for up
newState(dim) = min(max(state(dim)+dir,1),size(world,dim)); %increment state value on chosen dim, constraining to stay in world

function reward = getReward(world,state,action)
stateActionString = ''; %build text representation of state coordinates followed by action (workaround for limitations of matlab syntax)
for d=1:length(state)
    stateActionString = [stateActionString num2str(state(d)) ','];
end
stateActionString = [stateActionString num2str(action)];
reward = eval(['world(' stateActionString ')']); %reward of taking action in state