- 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.
For-loop instantly jump to largest value
2 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
Stephen23
2017 年 9 月 14 日
編集済み: Stephen23
2017 年 9 月 14 日
Some points to consider:
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.
回答 (4 件)
Andrei Bobrov
2017 年 9 月 15 日
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)
0 件のコメント
Rohan Kale
2017 年 9 月 14 日
編集済み: Rohan Kale
2017 年 9 月 14 日
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
0 件のコメント
Stephen23
2017 年 9 月 14 日
編集済み: Stephen23
2017 年 9 月 15 日
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
0 件のコメント
Image Analyst
2017 年 9 月 15 日
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.
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!