how to get elements of an array from a matrix in matlab
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
% Compute the optimal value function using the Value Iteration algorithm.
for runs=1:1000
for m=1:N1
for n=1:N2
for p =1:length(d)
% Take all possible actions.
global action
action = d(p);
s_next = [x1(m); x2(n)]+ 0.01*tank(0,[x1(m); x2(n)]);
% Compute the closest discretized state.
[r,s] = closest(s_next);
nextV(p) = V(r,s);
%
end
% [V_best,best_ind] = max(nextV);
%
% % Improve value function estimate using Bellman's equation.
% V(m,n)= Reward([x1(m); x2(n)])+gamma*V_best ;
end
end
end
Error:
Unable to perform assignment because the left and right
sides have a different number of elements.
Error in ANN_RL (line 50)
nextV(p) = V(r,s);
0 comentarios
Respuestas (1)
Walter Roberson
el 30 de Dic. de 2021
[r,s] = closest(s_next);
nextV(p) = V(r,s);
That code could fail if V() is a function that is not returning a scalar.
That code could also fail if V is an array, but closest() is returning a non-scalar. For example what does closest() do if there are two locations that are the same distance apart?
3 comentarios
Walter Roberson
el 30 de Dic. de 2021
r = find(abs(x1-s_next(1)) <= delta1,1);
it is not obvious to me at the moment that that find() will definitely find any locations. Before
nextV(p) = V(r,s);
you should put in
assert(isscalar(r), 'r is not scalar')
assert(isscalar(s), 's is not scalar')
Walter Roberson
el 30 de Dic. de 2021
You have floating point roundoff problems. Your delta2 is half of the distance between your x2 values.
If your x2 values were exactly evenly spaced in floating point representation, then all s_next2(2) input values within the range should be within half nominal distance between samples. However, your x2 are not equally spaced in floating point representation
K>> fprintf('%.999g\n', unique(diff(x2)))
1.0101010101009961772433598525822162628173828125
1.01010101010100328267071745358407497406005859375
1.010101010101006835384396254085004329681396484375
1.0101010101010086117412356543354690074920654296875
1.01010101010100949991965535446070134639739990234375
1.010101010101009944008865204523317515850067138671875
1.0101010101010101660534701295546256005764007568359375
1.010101010101010388098075054585933685302734375
1.010101010101013940811753855086863040924072265625
1.01010101010101749352543265558779239654541015625
Those are hex 3ff0295fad40a540 to 3ff0295fad40a5a0 -- a variety of values, not even just single bit errors.
Because of this it is possible to find s_next2(2) values that are not within delta2 of any value in x2 .
I recommend you switch to
s = interp1(x2, 1:length(x2), s_next(2), 'nearest');
If you do
s = interp1(x2, 1:length(x2), s_next(2), 'nearest', 'extrap');
then you do not need to check whether s_next(2) is greater than the end of the array (or, for that matter, before the beginning of the array): extrap will cause the first or last index to be returned as appropriate.
So you could replace closest with
% computes the closest discretized state to snext
function [r,s] = closest(s_next)
global x1;
global x2;
r = interp1(x1, 1:length(x1), s_next(1), 'nearest', 'extrap');
s = interp1(x2, 1:length(x2), s_next(2), 'nearest', 'extrap');
end
Ver también
Categorías
Más información sobre Matrices and Arrays en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!