How to automate the sequence of code?

2 Ansichten (letzte 30 Tage)
yashvin
yashvin am 14 Jul. 2015
Beantwortet: Walter Roberson am 15 Jul. 2015
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

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 15 Jul. 2015
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

Weitere Antworten (0)

Kategorien

Mehr zu Multidimensional Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by