How to automate the sequence of code?

1 次查看(过去 30 天)
Hi I am planning to automate this piece of code which is below:
A=[1 2 3 4 5 6 7 8 9 10]
len_A=length(A)=10
if (A(3)>A(2))
Num_mean= mean(A(1):A(3))
if (A(5)>A(4))
Num_mean= mean(A(3):A(5))
if (A(7)>A(6))
Num_mean= mean(A(5):A(7))
if (A(9)>A(8))
Num_mean= mean(A(7):A(9))
Num_mean= mean(A(9):A(10))
else
Num_mean= mean(A(7):A(8))
end
end
end
end
The length of A is always greater than 2 and it is always even.
Here is another example
A=[1 2 3 4 5 6]
len_A=length(A)=6
if (A(3)>A(2))
Num_mean= mean(A(1):A(3))
if (A(5)>A(4))
Num_mean= mean(A(3):A(5))
Num_mean= mean(A(5):A(6))
else
Num_mean= mean(A(3):A(4))
end
end
Any help in automating it for any numbers in array A and for any length? All the values should be stored in Num_mean

采纳的回答

Walter Roberson
Walter Roberson 2015-7-15
If all of the answers are to be stored in Num_mean then why do you bother calculating the means until you know they will be used?
if A > B
x = calculation
if C > D
x = calculation2
end
end
is faster as
if A > B
if C > D
x = calculation2
else
x = calculation1
end
end

更多回答(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