How to create an array from array of numbers??

2 views (last 30 days)
I've got a code which works but struggling to simplify it down and still learning .
This is the code I've got:
thrust_data=load('thrust.mat', '-ascii');
thrust.times=thrust_data(:,1);
thrust.values=thrust_data(:,2);
glider.weight = 3;
acc=thrust.values/(glider.weight/9.81);
for i=1:length(thrust.times)
if thrust.times(i)<=1
Base_Time=0.1;
Extra_Time=0;
elseif thrust.times(i)>1.4
Base_Time=0.1;
Extra_Time=0;
elseif thrust.times(12,1)
Base_Time=0.1;
Extra_Time=0.1;
elseif thrust.times(13,1)
Base_Time=0.1;
Extra_Time=0.1;
end
dt(i)=Base_Time+Extra_Time;
end
speed_1=acc(2,1)*dt(2);
speed_2=speed_1+acc(3,1)*dt(3);
speed_3=speed_1+acc(4,1)*dt(4);
speed_4=speed_1+acc(5,1)*dt(5);
speed_5=speed_1+acc(6,1)*dt(6);
speed_6=speed_1+acc(7,1)*dt(7);
speed_7=speed_1+acc(8,1)*dt(8);
speed_8=speed_1+acc(9,1)*dt(9);
speed_9=speed_1+acc(10,1)*dt(10);
speed_10=speed_1+acc(11,1)*dt(11);
speed_11=speed_1+acc(12,1)*dt(12);
speed_12=speed_1+acc(13,1)*dt(13);
speed_13=speed_1+acc(14,1)*dt(14);
speed_array=[0 speed_1 speed_2 speed_3 speed_4 speed_5 speed_6 speed_7 speed_8 speed_9 speed_10 speed_11 speed_12 speed_13];
With the thrust.mat numbers being (time on left with thrust on right):
0 0
0.1 1
0.2 1.8
0.3 2.4
0.4 2.8
0.5 3
0.6 3
0.7 3
0.8 3
0.9 3
1 2.9
1.2 2.4
1.4 1.2
1.5 0
It works but there must be a shorter simplier way to do the speed section but struggling, can anyone give me a helping hand please??

Answers (2)

James Tursa
James Tursa on 30 Apr 2019
Are you sure this:
speed_1=acc(2,1)*dt(2);
speed_2=speed_1+acc(3,1)*dt(3);
speed_3=speed_1+acc(4,1)*dt(4);
speed_4=speed_1+acc(5,1)*dt(5);
:
shouldn't be this instead (adding to previous value instead of first value):
speed_1 = acc(2,1)*dt(2);
speed_2 = speed_1 + acc(3,1)*dt(3); % adds to speed1
speed_3 = speed_2 + acc(4,1)*dt(4); % adds to speed2
speed_4 = speed_3 + acc(5,1)*dt(5); % adds to speed3
:
If so, looks like a cumsum of acc.*dt is what you want.

Steven Lord
Steven Lord on 30 Apr 2019
for i=1:length(thrust.times)
if thrust.times(i)<=1
Base_Time=0.1;
Extra_Time=0;
elseif thrust.times(i)>1.4
Base_Time=0.1;
Extra_Time=0;
elseif thrust.times(12,1)
Base_Time=0.1;
Extra_Time=0.1;
elseif thrust.times(13,1)
Base_Time=0.1;
Extra_Time=0.1;
end
dt(i)=Base_Time+Extra_Time;
end
This can be replaced using logical indexing.
condition1 = thrust.times <= 1;
condition2 = ~condition1 & thrust.times > 1.4;
% Preallocate dt to be the correct size
dt = zeros(size(thrust.times));
% Fill in the elements of dt that satisfy the conditions
dt(condition1) = 0.1 + 0;
dt(condition2) = 0.1 + 0;
I'm not completely certain about your third and fourth conditions. They don't depend upon your loop variable, so depending on thrust.times(12, 1) and thrust.times(13, 1) either one (or both) of them are always satisfied (in which case you should add 0.2 to dt on the line where I preallocated it, to fill all the other values to 0.2) or neither is ever satisfied (in which case leaving it preallocated with 0's is correct.)
I agree with James Tursa's suggestion about using cumsum to create the cumulative sum if each of your numbered speed variables should be added to the previous variable instead of being added to the first.
If in fact each element of acc(n, 1)*dt(n) should be added to the first speed, then omit the cumsum and just use acc.*dt then add the first speed to the resulting vector.

Categories

Find more on Sparse Matrices 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!