Breaking Vector into Subvectors

조회 수: 5 (최근 30일)
Joshua Peters
Joshua Peters 2021년 1월 23일
답변: Prahlad Gowtham Katte 2022년 2월 15일
How can I evenly split a vector into subvectors. My problem is, in my code I am using dde23 with some randomness involved. I am trying to obtain the coordinates of the numerical solution in intervals of 1 second. The randomness I include however changes the length of the time vector and hence the coordinates of the numerical solution everytime I rund the code. Is there a nice way where I can split the solution vector into subvectors that correspond to 1 second time intervals whilst including this randomness? My current code is as seen below.
Here sol.y corresponds to the coordinates at each time which is in the sol.x vector. I am wanting to split the Car1, Car2 and Car3 vector into intervals corresponding to 1 second.
%3 Cars
%Defines the time delay for each x
lags = [1 1 1];
%Creates a vector of times
tspan = [0 10];
rng('shuffle')
sol = dde23(@ddefun, lags, @history, tspan);
hold on
plot(sol.x,sol.y,'-')
grid on
xlabel('Time (s)');
ylabel('Velocity (m/s)');
legend('Car 1','Car 2','Car 3','Location','NorthWest');
Car1=sol.y(1,:);
Car2=sol.y(2,:);
Car3=sol.y(3,:);
function dydt = ddefun(t,x,Z)
a = -1;
b = 1;
alpha =0.5;
s=rng;
R=rand(1,1);
rng(s);
%Generates a random number between -1 and 1
omega= (b-a).*R + a;
%Calculates a value slowing down/speeding up car 1
gamma=-1/2+mod(t+pi*omega*t,1);
ylag1 = Z(:,1);
ylag2 = Z(:,2);
ylag3 = Z(:,3);
%Specifies system of equations
dydt = zeros(3,1);
dydt=[gamma*x(1);alpha*(ylag1(1)-ylag2(2));alpha*(ylag2(2)-ylag3(3))];
end
%Gives initial velocity profiles
function s = history(t)
s = [20 20 20];
end
  댓글 수: 1
Joshua Peters
Joshua Peters 2021년 1월 23일
Note: I have attempted using mat2cell but I am finding that this has issues when the vecors change length every time the code is run. This is because it is not guaranteed that the vectors can be split evenly.

댓글을 달려면 로그인하십시오.

답변 (1개)

Prahlad Gowtham Katte
Prahlad Gowtham Katte 2022년 2월 15일
Hello,
As per my understanding of the query, you are trying to split the vector into sub vectors. For this you can first get the index array of vector where index (i) corresponds to indices in X axis where elements lie in between i-1 and i. Then using those indices you can create a cell array for “Car 1”, “Car2” and “Car 3“.
The following modified code will help in achieving the desired functionality.
%3 Cars
%Defines the time delay for each x
lags = [1 1 1];
%Creates a vector of times
tspan = [0 10];
rng("shuffle")
sol = dde23(@ddefun, lags, @history, tspan);
hold on
plot(sol.x,sol.y,'-')
grid on
xlabel("Time (s)");
ylabel("Velocity (m/s)");
legend("Car 1","Car 2","Car 3","Location","NorthWest");
Car1=sol.y(1,:);
Car2=sol.y(2,:);
Car3=sol.y(3,:);
%%% Here is the code I've added
maximum=max(sol.x); %Find the maximum value of X axis
Car1_split={};
Car2_split={};
Car3_split={};
for j=1:maximum
if j==1
l=sol.x>=j-1; %Including 0 in the 1st sub array
else
l=sol.x>j-1;
end
m=sol.x<=j;
n=l&m;
idx=find(n);%Finding Indices of the elements satisfying above conditions
%Appending selected Car1,2,3 coressponding indices to the split cell array.
Car1_split=[Car1_split Car1(idx)];
Car2_split=[Car2_split Car2(idx)];
Car3_split=[Car3_split Car3(idx)];
end
function dydt = ddefun(t,x,Z)
a = -1;
b = 1;
alpha =0.5;
s=rng;
R=rand(1,1);
rng(s);
%Generates a random number between -1 and 1
omega= (b-a).*R + a;
%Calculates a value slowing down/speeding up car 1
gamma=-1/2+mod(t+pi*omega*t,1);
ylag1 = Z(:,1);
ylag2 = Z(:,2);
ylag3 = Z(:,3);
%Specifies system of equations
dydt = zeros(3,1);
dydt=[gamma*x(1);alpha*(ylag1(1)-ylag2(2));alpha*(ylag2(2)-ylag3(3))];
end
%Gives initial velocity profiles
function s = history(t)
s = [20 20 20];
end
For better understanding of the used functions please refer to the following links.
Hope it helps.

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by