Replace certain elements of vector with the values from another vector
3 views (last 30 days)
Show older comments
Myrto Kasioumi
on 25 May 2020
Commented: Myrto Kasioumi
on 26 May 2020
Hello!
I have 2 vectors with the same length: S_1 and S_2
I wanna substitute some values of S_1 with values of S_2 for some specific positions. For example I wanna substitute the 3d value of S_1 with the 3d value of S_2 and so on. I have saved the positions in another vector called m.
My code so far is:
syms S(t) P(t) c(t) z(t) A b g v k u d r w t Y
eqn1 = diff(S,t) == A*(b*S)^(1-g)*(z^g) - k*z - c - b*S*(k+1);
eqn2 = diff(P,t) == u*z - d*P + (1-b)*S;
eqn3 = diff(c,t) == (((1-b)/u) * (v - A*g*(b*S)^(1-g)*z^(g-1)) + b * (A*(1-g)*b^(-g)*S^(-g)*b*z^(g) -k -1) - r) * c;
eqn4 = diff(z,t) == (((A*g*(b*S)^(1-g)*z^(g-1)-v)/(A*g*(g-1)*(b*S)^(1-g)*z^(g-2))) * (d + (((1-w)*c*u)/(w*P*(v-A*g*(b*S)^(1-g)*z^(g-1)))) + (b*(A*(1-g)*b*(b*S)^(-g)*z^g - k - 1)) + (((1-b)*(v-A*g*(b*S)^(1-g)*z^(g-1)))/u))) - (((b*A*g*b*(1-g)*(b*S)^(-g)*z^(g-1))/(A*g*(g-1)*(b*S)^(1-g)*z^(g-2))) * (A*(b*S)^(1-g)*z^g - v*z - c - b*S*(k+1)));
[VF,Subs] = odeToVectorField(eqn1,eqn2,eqn3,eqn4);
odefcn = matlabFunction(VF, 'Vars',{t,A,Y,b,d,g,k,r,u,v,w});
A=3;
b=0.4;
g=0.3;
v=2;
k=4;
u=10;
d=0.2;
r=0.5;
w=0.7;
tspan = [0 6000];
y0 = [100 60 30 50];
[t,Y] = ode45(@(t,Y) odefcn(t,A,Y,b,d,g,k,r,u,v,w) , tspan, y0);
P_1 = Y(:,1);
S_1 = Y(:,2);
c_1 = Y(:,3);
z_1 = Y(:,4);
x_1 = b*S;
sP = size(P_1);
MPx = (1-g)*(b*A*S_1).^(-g).*z_1.^g*b.*A;
MPz = g*(b*A*S_1).^(1-g).*z_1.^(g-1);
m = find(MPx(:,1)<MPz(:,1));
sm = size(m);
g=0.7;
tspan2 = linspace(0, 1000, sP(1));
n0 = [100 60 30 50];
[t,N] = ode45(@(t,N) odefcn(t,A,N,b,d,g,k,r,u,v,w) , tspan2, n0);
P_2 = N(:,1);
S_2 = N(:,2);
c_2 = N(:,3);
z_2 = N(:,4);
x_2 = b*S;
for j=m(1):m(sm(1))
S_1(j,1)=S_2(j,1)
end
Now for the values that MPx is smaller than MPz I wanna substitute the values of S_1 with the values of S_2. For example if MPx < MPz in the third entry then I wanna set the 3d value of S_1 to be equal to the 3d value of S_2.
Any help would be appreciated.
Thanks in advance!
2 Comments
Ruben Lange
on 25 May 2020
Edited: Ruben Lange
on 25 May 2020
Hi!
Could you indeed upload your entire code?
Also, could you specify which entries in the vectors S_1 and S_2 you want to change? I don't know what the criteria are...
Thanks
Accepted Answer
Ameer Hamza
on 25 May 2020
There is no need for a for-loop
S_1(m) = S_2(m);
3 Comments
Ameer Hamza
on 26 May 2020
The element of vector 'm' is above 281. However, there are just 66 elements in S_2. You cannot access more than 66 elements from S_2.
More Answers (0)
See Also
Categories
Find more on Bartlett in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!