How to add value to matrix?

2 ビュー (過去 30 日間)
ryunosuke tazawa
ryunosuke tazawa 2021 年 10 月 13 日
コメント済み: ryunosuke tazawa 2021 年 10 月 13 日
I want to add elements to matrix.
I want to add the calculated value to the end. In the code below, I would like to add the calculated value of R to RR.
I tried R = []; RR (end + 1) = R ;, but only one value was added.
I want to add all the R calculated in one simulation to the RR.(R is one value.)
% Define of step function at each step in reinforcement learning
function [NextObservation, Reward, IsDone, LoggedSignals] = myStepfunction(Action,LoggedSignals,SimplePendulum)
statePre = [-pi/2;0];
statePre(1) = SimplePendulum.Theta;
statePre(2) = SimplePendulum.AngularVelocity;
IsDone = false;
SimplePendulum.pstep(Action);
state = [-pi/2;0];
state(1) = SimplePendulum.Theta;
state(2) = SimplePendulum.AngularVelocity;
X_state_Position = sin(state(1));
Y_state_Position = -cos(state(1));
RR = [];
R = ones(1,1);
Ball_Target = 10;
Ball_Distance = X_state_Position + (-state(2))* sqrt(2*abs(Y_state_Position)/9.8);
R = -abs(Ball_Distance -Ball_Target);
RR(end+1) = R; 
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 = +R;
end
  2 件のコメント
KSSV
KSSV 2021 年 10 月 13 日
There is no loop why you want to use end+1 or indexing?
ryunosuke tazawa
ryunosuke tazawa 2021 年 10 月 13 日
I tried to use for loop.
But reslut was -4.4274 -4.4274 -4.4274 -4.4274 -4.4274 -4.4274 -4.4274 -4.4274 -4.4274 -4.4274 -4.4274 -4.4274 -4.4274 -4.4274 -4.4274 -4.4274 -4.4274 -4.4274 -4.4274 -4.4274 -4.4274 -4.4274.
Although the value of R is constantly updated, only one value is added to RR.
RR = [];
for i=1:400
R = -abs(Ball_Distance -Ball_Target);
RR(end+1) = R; 
end

サインインしてコメントする。

回答 (0 件)

カテゴリ

Help Center および File ExchangeSparse Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by