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 个评论

Think you're going to have to illustrate the result you wish and how it relates to the above array.
  1. Use a suitable title for the thread subject and
  2. Do NOT use contributor names for tags.
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?
Firstly the array is divided array into two, the first part end on the 10th element which 4.0 (i = 10) I need to a code that will select the element from 10th element based on the condition that if 10th element is greater than 9th element we select 10th element, we compare 8th and 9th element if the 9th is greater we select 9th, if not we go the next until is the condition is met (preceding is greater than the previous)
The second part is start from 11th (i=4.2) element and end on 23rd (i=2.5) element the code will select based on a condition that if 11th element is greater than 12th element we select 11th element, we move a step further by comparing 12th element and 13th element if 12th element is greater 13th then we select 12th until we get to the end of array(Previous element is greater than preceding)
Still not sure what select means. Do you want to extract the "selected" element to a new variable?
And "close the loop" means.......???
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).
This is another way to explain the problem, starting from the first number in the array (1.1) I want to select the number incrementally to 4.0 (10th element) which is the 10th element 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]

请先登录,再进行评论。

回答(1 个)

Use FOR loop. If you want to write a loop with a variable i from 10 to 2, write like:
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 个评论

The first part of the question is a backward loop from the 10th element to 2nd element, (that is i index), j = i-1, if i > j then pick i,
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!

Translated by