For-loop instantly jump to largest value

2 views (last 30 days)
Olle Haglund
Olle Haglund on 14 Sep 2017
Commented: Image Analyst on 15 Sep 2017

Hi! i have a problem with a for loop. The problem is that the code jumps right to rpm1<9500 instead of doing all the ones above, since all rpm1 values are <9500. Therefore i just get a long vector of zeros. Any ideas on how to fix this?

for i=rpm1;
if rpm1<750;
torque=287.91;
tv1=[tv1 torque];
elseif rpm1<1250;
torque=345,51;
tv1=[tv1 torque];
elseif rpm1<1775;
torque=368,55;
tv1=[tv1 torque];
elseif rpm1<2300;
torque=397,35;
tv1=[tv1 torque];
elseif rpm1<2740;
torque=500,94;
tv1=[tv1 torque];
elseif rpm1<3140;
torque=652,95;
tv1=[tv1 torque];
elseif rpm1<3550;
torque=798,12;
tv1=[tv1 torque];
elseif rpm1<4065;
torque=798,12;
tv1=[tv1 torque];
elseif rpm1<4705;
torque=900;
tv1=[tv1 torque];
elseif rpm1<5325;
torque=884,43;
tv1=[tv1 torque];
elseif rpm1<5860;
torque=855,09;
tv1=[tv1 torque];
elseif rpm1<6340;
torque=808,47;
tv1=[tv1 torque];
elseif rpm1<6865;
torque=763,56
tv1=[tv1 torque];
elseif rpm1<7365;
torque=703,08;
tv1=[tv1 torque];
elseif rpm1<7775;
torque=644,31;
tv1=[tv1 torque];
elseif rpm1<8210;
torque=564,84;
tv1=[tv1 torque];
elseif rpm1<8450;
torque=352,44;
tv1=[tv1 torque];
elseif rpm1<8750;
torque=287,91;
tv1=[tv1 torque];
else rpm1<9500;
torque=0;
tv1=[tv1 torque];
end
end
  1 Comment
Stephen23
Stephen23 on 14 Sep 2017
Edited: Stephen23 on 14 Sep 2017
Some points to consider:
  • You define a loop with one iteration, and then complain that it has only one iteration.
  • You define the loop variable to be i but never use this anywhere, or in anyway increment any variable within the loop.
  • You used commas for the decimal radix, which is not supported by MATLAB.
  • You apparently generate a vector by concatenating new elements inside the loop. This is inefficient.
  • You wrote a huge long list of elseif statements. This is a pointlessly complex way to write code.
Learn to write efficient MATLAB code: put all of the values into one matrix:
M = [...
0750,287.91;
1250,345.51;
1775,368.55;
2300,397.35;
2740,500.94;
3140,652.95;
3550,798.12;
4065,798.12;
4705,900.00;
5325,884.43;
5860,855.09;
6340,808.47;
6865,763.56
7365,703.08;
7775,644.31;
8210,564.84;
8450,352.44;
8750,287.91;
9500,000.00];
and then learn about such useful functions as interp and bsxfun, and useful operations as logical indexing. Your code will be simpler and much more efficient.

Sign in to comment.

Answers (4)

Andrei Bobrov
Andrei Bobrov on 15 Sep 2017
x = [750 287.91
1250 345.51
1775 368.55
2300 397.35
2740 500.94
3140 652.95
3550 798.12
4065 798.12
4705 900
5325 884.43
5860 855.09
6340 808.47
6865 763.56
7365 703.08
7775 644.31
8210 564.84
8450 352.44
8750 287.91
9500 0];
rpm1 = randi([600 9500],20,1);
out = x(discretize(rpm1,[-inf;x(:,1)]),2)

Rohan Kale
Rohan Kale on 14 Sep 2017
Edited: Rohan Kale on 14 Sep 2017
Your for loop construct itself is incorrect. It should be
for rpm1 = a:b:c
where a and c are start and end limits and b is increment

Stephen23
Stephen23 on 14 Sep 2017
Edited: Stephen23 on 15 Sep 2017
You do not need such pointlessly complex code using lots of elseif as if MATLAB was an ugly low-level language like C++. Keep your data in matrices or arrays. This should get your started on writing much better code:
>> M = [...
0750,287.91;
1250,345.51;
1775,368.55;
2300,397.35;
2740,500.94;
3140,652.95;
3550,798.12;
4065,798.12;
4705,900.00;
5325,884.43;
5860,855.09;
6340,808.47;
6865,763.56
7365,703.08;
7775,644.31;
8210,564.84;
8450,352.44;
8750,287.91;
9500,000.00];
>> vec = 1000:500:9000; % points you want to find values for
>> out = interp1(M(:,1),M(:,2),vec(:))
out =
316.71
356.48
380.89
444.44
599.75
780.42
798.12
867.37
892.59
874.83
841.49
794.78
747.23
683.73
603.2
341.69
191.94

Image Analyst
Image Analyst on 15 Sep 2017
When you set the loop iterator, i, equal to a vector rpm1, the iterator, i, takes on all the values in the vector, rpm1. So you need to check i, not rpm1, like this:
for i=rpm1
if i < 750
torque = 287.91;
tv1 = [tv1 torque];
elseif i < 1250
torque = 345,51;
tv1 = [tv1 torque];
By the way, you don't use semicolons at the end of ifs, elseifs, or ends.
  3 Comments
Stephen23
Stephen23 on 15 Sep 2017
Edited: Stephen23 on 15 Sep 2017
for i = length(rpm1)
??? Surely:
for i = 1:length(rpm1)
Image Analyst
Image Analyst on 15 Sep 2017
Of course, sorry - good catch, thanks.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!