How to write this loop?
显示 更早的评论
I have an array
[ 1.1 0.5 1.8 2.5 0.6 2.0 3.1 3.3 3.5 4.0 4.2 4.7 5.0 4.2 2.2 3.2 3.4 3.5 3.3 4.8 1.11 2.0 2.5]
start a loop from i=10 backward to i=2, if i is greater than any of j, j is from i-1 to i=1, then pick this i, close the loop. start another loop from i=11 to i=22, if i is greater than j, j is from i+1 to i=23, then pick this i, close the loop.
This is another way to explain the problem, starting from the first number in the array (1.1) to 4.0 which is the 10th element I want select number incrementally then end the loop there. Start another loop from the other end 2.5 ( the 23rd element ) incrementally back to the 11th element (4.0). the expected outcome is [1.1 1.8 2.5 3.1 3.3 3.5 4.0 5.0 4.8 2.5]
8 个评论
dpb
2017-8-14
Think you're going to have to illustrate the result you wish and how it relates to the above array.
- Use a suitable title for the thread subject and
- Do NOT use contributor names for tags.
Image Analyst
2017-8-15
What is j? Is it a vector? That's what I got from this: "if i is greater than any of j". I figure there is more than one number in j. Please give us the numbers in j, like
j = [1,13,42,175]; % or whatever j is.
What does "pick this i" mean? Do you mean save it into a new variable called something like picked_i? If so, why -- what do you do with the "picked" i?
What does "j is from i-1 to i=1" mean??? Does that mean you want to start another loop, this time with an index called j, which will blow away your previous vector of j (seems a weird thing to do).
What does "close the loop" mean??? Do you mean exit the loop, like with a break statement after the for loop over j?
What's the "use case" for all of this quirky operations anyway? Is it homework, or is there a real world need for this?
Olumide Omotere
2017-8-15
编辑:Image Analyst
2017-8-15
Image Analyst
2017-8-15
Still not sure what select means. Do you want to extract the "selected" element to a new variable?
And "close the loop" means.......???
Olumide Omotere
2017-8-15
It also isn't defined clearly what to do when the nth value isn't greater than the (n-1)th. What happens for those conditions; is the lesser selected or the positions skipped so the resulting vector will have fewer than N/2 (N=10 here, the size of the subset input)?
I've got other commitment at the moment so can't take the time to try to guess what the intent is enough to actually write a solution making assumptions about what is the intent, but look at
doc diff
doc sign
doc arrayfun
with an anonymous function...
N=10; % your breakpoint location
dx=[0 diff(x(1:N)); % successive differences of the input
ix=sign(dx)>0); % logical addressing vector for x(i+1)>x(i)
Now the only question is which of x(ix) are the ones you actually want to end up with--that is just not yet clear enough to write the selection criterion.
Obviously the same idea works for the other section with reverse sign (or fliplr the subsection and it's identical to the first half).
Olumide Omotere
2017-8-16
回答(1 个)
mizuki
2017-8-15
for i = 10:-1:2
end
This means i changes from 10 to 2 with an interval -1.
I did not get the first part of your question, but for the second part, "If i is greater than j, j is from i+1 to i=23," write code as:
if(i > j)
for j = i+1:23
end
end
2 个评论
Olumide Omotere
2017-8-15
mizuki
2017-8-15
Did you see my answer and run your code? A forward loop from 1 to 10 with interval 2 is
i = 1:2:10
<write your operation>
end
A backward loop from 10 to 1 with interval 2 is
for i = 10:-2:1
<write your operation>
end
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!