フィルターのクリア

How to pass control from one loop to the other loop

3 ビュー (過去 30 日間)
Sai
Sai 2015 年 10 月 14 日
編集済み: Stephen23 2015 年 10 月 16 日
My aim is to alter a data sample in such a way that the sign of values in between 0.01 to next 0.01 are altered.
Like for example; if given sample is
{0.3,0.21,0.9,0.3,0.01,0.3,0.4,0.32,0.01,0.9,0.97}
My code has to give the output as
{0.3,0.21,0.9,0.3,0.01,-0.3,-0.4,-0.32,0.01,0.9,0.97}
So, I'm trying to write a nested loop to read the sample and alter the sign in between 0.01 to next 0.01
for n=1:100000
if u(n)=0.01
for m=n+1:10000
if u(m)=0.01
% switch to the original 'n' loop
end
u(m)=-1*u(m);
end
end
end
But, I'm unable to switch to the original loop. Anyone suggest, how can I switch between them ?

採用された回答

Thorsten
Thorsten 2015 年 10 月 14 日
編集済み: Thorsten 2015 年 10 月 14 日
invert = 1;
for i = 1:numel(u)
if u(i) == 0.01
invert = -invert;
elseif invert<0
u(i) = -u(i);
end
end
  1 件のコメント
Sai
Sai 2015 年 10 月 15 日
Thanks for your answer, very simple and elegant :)

サインインしてコメントする。

その他の回答 (2 件)

Eng. Fredius Magige
Eng. Fredius Magige 2015 年 10 月 14 日
try this
for n=1:100000 if(find(sample(:)==0.01,1)=<find(sample(:)==0.01,2)) % sample is your data num=-1*sample(:,n) else num=sample(:,n) end
  1 件のコメント
Sai
Sai 2015 年 10 月 15 日
Thanks for your suggestion :). It worked

サインインしてコメントする。


Stephen23
Stephen23 2015 年 10 月 14 日
編集済み: Stephen23 2015 年 10 月 14 日
There is no need for slow and inefficient loops when code vectorization is faster and neater:
>> tol = 0.0005;
>> val = 0.01;
>> V = [0.3,0.21,0.9,0.3,0.01,0.3,0.4,0.32,0.01,0.9,0.97];
>> idx = abs(V-val)<tol;
>> idy = mod(cumsum(idx),2);
>> idz = (-1).^(idy&[false,idy(1:end-1)]);
>> V.*idz
ans =
0.3 0.21 0.9 0.3 0.01 -0.3 -0.4 -0.32 0.01 0.9 0.97
  2 件のコメント
Sai
Sai 2015 年 10 月 15 日
Thanks for your answer, You've introduced me to new concept. :)
Stephen23
Stephen23 2015 年 10 月 16 日
編集済み: Stephen23 2015 年 10 月 16 日
My pleasure. You can also vote for answers that introduce you to new concepts!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by