How to step-wise iterate for every element of a vector in a while-loop?
Mostra commenti meno recenti
vec= [1 1 1 1 3 1 4 1 1 5 5]';
myOffset=0.05;
nonOnes=find(vec>1); % Find elements > 1
N=vec(nonOnes); % Create vector with elements > 1
i=1;
n=1;
counter=1;
while n<=N(i)
myOrder(counter)=(n/(N(i)+1))*(1 + myOffset*randn(1));
n=n+1;
counter=counter+1;
end
Here is my problem: In the code, I wrote i=1 to make it program function somehow. But in reality "i" should be in the range of i=1:length(N), and I do not know how to make the wile loop function for every N(i). The vector myOrder should store 17 elements if the while loop iterates for each element N(i), but I get only 3 with this code. "n" starts with the value of one until it reaches the value of N(i).
4 Commenti
Jan
il 9 Ott 2017
Please use the "{} Code" button to format code properly: It is nicely indented, if the code lines start with two spaces.
I do not understand this:
The value of "i" should change with i=i+1 once the n reached the
value of % N(i) in the while-loop. In this case I'd like i to be
i=1:length(N)
So, didn't my answer in https://www.mathworks.com/matlabcentral/answers/360386-which-is-the-most-appropiate-loop-if-i-want-to-create-a-vector-myorder-n-with-random-values solve your problem?
If not, what is the problem with it? It probably only needs minor modifications to adapt to your new requirements (which don't seem to include your condition 1 anymore).
The new specification are still not very clear. Rather than giving us code that doesn't work give us an example of desired output, or explain step by step how that output is to be obtained.
And please, don't start another question for that. Edit one of these two.
Christian Muñoz
il 9 Ott 2017
Modificato: Christian Muñoz
il 9 Ott 2017
Christian Muñoz
il 9 Ott 2017
Risposta accettata
Più risposte (0)
Categorie
Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!