简体   繁体   中英

How to add value to matrix

I want to add value to matrix. But, I could not do it. I made the stepfunction for reinforcement learning and I calculated value which named R. I want to gather R of value and add to matrix named RR. However, all the calculated values are not added. Is this because it's done inside a function? If it's not in a function, it works. Please someone tell me the way to fix it.

% Define the step function
function [NextObservation, Reward, IsDone, LoggedSignals] = 
    myStepfunction(Action,LoggedSignals,SimplePendulum)

    % get the pre statement
    statePre = [-pi/2;0];
    statePre(1) = SimplePendulum.Theta;
    statePre(2) = SimplePendulum.AngularVelocity;
    
    IsDone = false;
   
    % updating states 
    SimplePendulum.pstep(Action);
    
    % get the statement after updating
    state = [-pi/2;0];                                  
    state(1) = SimplePendulum.Theta;               
    state(2) = SimplePendulum.AngularVelocity;      
 
    RR = []; 
    Ball_Target = 10;
    Ball_Distance = Ballfunction(SimplePendulum);
    R =  -abs(Ball_Distance -Ball_Target);  ← this the calculated value
    RR = [RR,R];     ← I want to add R to RR
    
    if (state(2) > 0) ||  (SimplePendulum.Y_Position < 0) 
        IsDone = true;
        [InitialObservation, LoggedSignal] = myResetFunction(SimplePendulum);
        LoggedSignal.State = [-pi/2 ; 0];
        InitialObservation = LoggedSignal.State;
        state = InitialObservation; 
        SimplePendulum.Theta =-pi/2;
        SimplePendulum.AngularVelocity = 0;
    end

    LoggedSignals.State = state;                  
    NextObservation = LoggedSignals.State;
    Reward =  +max(R);

end

If I understand you correctly, you are trying update RR with this function, and then use/see it outside the loop. This means you need to make two edits:

a) You need to pass RR in and out of the function to update it. You will need to change the first line of your code as shown here, but you will also need to change how you call the function:

function [NextObservation, Reward, IsDone, LoggedSignals, RR] = 
    myStepfunction(Action,LoggedSignals,SimplePendulum, RR)

b) When you do this RR = [] you delete the content of RR . To initialise RR you will need to move this line to the script that calls this function, making sure it is called before this function is ever called.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM