Apply some code on this array

1 次查看(过去 30 天)
Hi, I have an array
S=[ -2.8933 -2.3867 -11.5800 -17.3733 0 22.8400 32.9800 23.0533 6.4933 0.0000 27.2333 44.0333 30.2333 7.1333 0 40.2267 84.8867 138.0800 131.7400 -0.0000]
I need to apply these lines of code on every 5 values
[Max,MaxIdx]=max(S)
[Min,MinIdx]=min(S)
for n=1:5
code(n)=0
if n>=MinIdx & n<=MaxIdx || n<=MinIdx & n>=MaxIdx
if MaxIdx > MinIdx
code(n)=1
else if MinIdx > MaxIdx
code(n)=2
end
end
end
end
the result array code, should be 0 0 0 1 1 0 2 2 2 2 0 2 2 2 2 0 0 2 2 2

采纳的回答

Nicholas Galang
Nicholas Galang 2018-8-13
编辑:Nicholas Galang 2018-8-13
Looping through S and creating a temporary array every 5 values should do the trick.
for i=1:5:length(S)
temp=S(i:i+4);
[Max,MaxIdx]=max(temp);
[Min,MinIdx]=min(temp);
for n=1:5
index=i+n-1;
code(index)=0;
if n>=MinIdx & n<=MaxIdx || n<=MinIdx & n>=MaxIdx
if MaxIdx > MinIdx
code(index)=1;
else if MinIdx > MaxIdx
code(index)=2;
end
end
end
end
code
end
  3 个评论
Nicholas Galang
Nicholas Galang 2018-8-13
I'm not sure if I understand your complaint. This code snippet will only print out the final code array. If you want all the intermediate code arrays just remove the semicolon in this line,
code(index)=0;
this line
code(index)=1;
and this line.
code(index)=2;
Lanceric Tse
Lanceric Tse 2018-8-13
Got it now, thanks very much

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by