How to step-wise iterate for every element of a vector in a while-loop?

1 次查看(过去 30 天)
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 个评论
Christian Muñoz
Christian Muñoz 2017-10-9
编辑:Christian Muñoz 2017-10-9
Thank you for your feedback, Jan. I will use the "{} Code" in the future. What I mean is that i should be in the range of 1 to 4 {i=1:length(N)}.
I apologise for that, Guillaume. I wanted to be more specific on my question since as you said.. my question was not clear. I will be more careful in the future. Tanks!
Christian Muñoz
Christian Muñoz 2017-10-9
Dear jan and Guillaume, I made some changes. I hope the question becomes a little bit clearer. Thank you so much. I learn from every mistake :)

请先登录,再进行评论。

采纳的回答

Christian Muñoz
Christian Muñoz 2017-10-10
After some hours, I think I found the solution. I do not think it is a beautiful solution, but it makes the job :)
vec= [1 1 1 1 3 1 4 1 1 5 5];
myOffset=0.05;
nonOnes=find(vec>1);
N=vec(nonOnes);
i=1;
n=1;
counter=1;
while n/N(i)<=1
myOrder(counter)=( (n/N(i)) )*(1 + myOffset*randn(1) );
n=n+1;
counter=counter+1;
if n>N(i)
n=1;
i=i+1;
end
if i>length(N)
break
end
end

更多回答(0 个)

类别

Help CenterFile 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