How can I split a vector in a particular way

Hello. I have this sequence:
nn=length(0:10:720);
M_motor=zeros(nn,1);
for alfa=0:10:720
cont=alfa/10+1;
cos_alfa=cosd(alfa);
alfa_cont(cont)=alfa;
x(cont)=S*((1-cos_alfa)/2+lambda_prim/4-(lambda_prim/4)*cos_alfa^2);
if (alfa<=180)
P(cont)=pa;
elseif (alfa<=360)
P(cont)=k1/((x(cont)+s)^mc);
elseif (alfa<=540)
P(cont)=k2/((x(cont)+s)^md);
else
P(cont)=pr;
end
beta2(cont)=asind(lambda2*sind(alfa));
F_alfa(cont)=((P(cont)-p0)*Ap/100)+(-(mj*r_maniv*((pi*n/30)^2)*(cosd(alfa)+lambda2*cosd(2*alfa)))/10000);
M_motor(cont)=(F_alfa(cont)*(sind(alfa+beta2(cont))/cosd(beta2(cont)))*r_maniv)/1000;
end
How can i break M_motor in 4 parts(subvectors) like this: M_motor1=values from 0-180 M_motor2=values from 180-360 M_motor3=values from 360-540 M_motor4=values from 540-720 Notice that the last value is the first value in the next vector, and then i want to add them all together like this: M_motor_t=M_motor1+M_motor2+M_motor3+M_motor4 (vector contains 19 values).

Answers (1)

You can extract values of M_motor into separate variables like this:
% Create sample data:
M_motor = randi(740, 1,50)
% Now start extracting ranges.
indexesToExtract = M_motor > 0 & M_motor <= 180;
M_motor1 = M_motor(indexesToExtract)
indexesToExtract = M_motor > 180 & M_motor <= 360;
M_motor2 = M_motor(indexesToExtract)
indexesToExtract = M_motor > 360 & M_motor <= 540;
M_motor3 = M_motor(indexesToExtract)
indexesToExtract = M_motor > 540& M_motor <= 720;
M_motor4 = M_motor(indexesToExtract)
% Combine all together
M_motor_t = [M_motor1, M_motor2, M_motor3, M_motor4]

6 Comments

Thank you very much, but it didn't work. It messed up the hole program. And where do I put the sequence, in or outside the for loop? Any other ideas, I think there are other solution that are much easier for a beginner like me. And by the way, M_motor1 should start from 180, not >180. Thanks and keep up the good work.
Jan
Jan on 22 Apr 2013
Edited: Jan on 22 Apr 2013
@Cristian: The method shown by Image Analyst are the most efficient and clear solution you can get. As usual in Matlab this "vectorized" method does not need a loopm because it processes the vector "at once".
I cannot believe that it is too hard to replace "M_motor > 180" by "M_motor >= 180" by your own, even if you are a beginner. I suggest to trust the very high skilled Matlab profi Image Analyst that this method is clean, fast and easy to adjust. I do not think, that there better solutions. Ok?
I replaced all the unknown terms and the program is ready to compile. See for yourself that something is not right:
nn=length(0:10:720);
x=zeros(nn,1);
P=zeros(nn,1);
beta2=zeros(nn,1);
Falfa=zeros(nn,1);
alfa_cont=zeros(nn,1);
F_alfa=zeros(nn,1);
M_motor=zeros(nn,1);
for alfa=0:10:720
cont=alfa/10+1;
cos_alfa=cosd(alfa);
alfa_cont(cont)=alfa;
x(cont)=76.8*((1-cos_alfa)/2+0.28/4-(0.28/4)*cos_alfa^2);
if (alfa<=180)
P(cont)=0.9;
elseif (alfa<=360)
P(cont)=320.612/((x(cont)+8.93023)^1.32);
elseif (alfa<=540)
P(cont)=1119.9/((x(cont)+8.93023)^1.24);
else
P(cont)=1.1;
end
beta2(cont)=asind(0.277778*sind(alfa));
F_alfa(cont)=((P(cont)-1)*5026.55/100)+(-(0.490088*38.4*((pi*6000/30)^2)*(cosd(alfa)+0.277778*cosd(2*alfa)))/10000);
M_motor(cont)=(F_alfa(cont)*(sind(alfa+beta2(cont))/cosd(beta2(cont)))*38.4)/1000;
end
figure(13)
plot(alfa_cont,M_motor,'-'),title('Momentul motor'),xlabel('\alpha [^\circRAC]'),ylabel('M [daN*m]'),grid;
fprintf('%8s\t\t%8s\n', 'alfa', 'M[daN*m]' )
fprintf('%8.4f\t\t%8.4f\n',[alfa_cont, M_motor]')
fprintf('\n')
M_motor=randi(720,1,50);
indexesToExtract=M_motor>=0 & M_motor<=180;
M_motor1=M_motor(indexesToExtract);
indexesToExtract=M_motor>=180 & M_motor<=360;
M_motor2=M_motor(indexesToExtract);
indexesToExtract=M_motor>=360 & M_motor<=540;
M_motor3=M_motor(indexesToExtract);
indexesToExtract=M_motor>=540 & M_motor<=720;
M_motor4=M_motor(indexesToExtract);
M_motor_t=[M_motor1, M_motor2, M_motor3, M_motor4];
fprintf('%8.4f\n',[M_motor_t]')
The results should be:
0 -> 0
10 -> 7,260285
20 -> 3,610095
30 -> -7,51374
40 -> -17,8892
50 -> -22,7603
60 -> -21,0049
70 -> -13,4732
80 -> -1,80943
90 -> 12,14926
100 -> 26,57698
110 -> 39,6486
120 -> 49,50641
130 -> 54,37124
140 -> 52,88591
150 -> 44,6914
160 -> 31,11781
170 -> 15,30426
180 -> 0
Um, obviously you have to take out the line
M_motor=randi(720,1,50);
where I generated random data for testing. Otherwise it just overwrites the M_motor you took such trouble to create.
Cristian
Cristian on 22 Apr 2013
Edited: Cristian on 22 Apr 2013
I think you don't understand me, I don't need the values that are >=0 and <=180, I need the values for alfa=0:10:180 in a vector M_motor1, values for alfa=180:10:360 in a vector M_motor2, values for alfa=360:10:540 in M_motor3, and values for alfa=540:10:720 in M_motor4. The problem for me is that i cannot break the M_motor in 4 equal parts, I need that the last value of M_motor1 that is 0 to be also be the first value in M_motor2. The same for the rest. So every subvector will have 19 values. And in the last part I want to add add the vectors M_motor1,2,3,4 in a M_motor_t. So the first sum in M_motor_t (first value) will be 0, next 7.26 etc. like i wrote above, in my last comment.
So why can't you do
M_motor1 = M_motor(0 : 10 : 180);
and so on??

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 21 Apr 2013

Community Treasure Hunt

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

Start Hunting!