フィルターのクリア

How to automate the sequence of code?

2 ビュー (過去 30 日間)
yashvin
yashvin 2015 年 7 月 14 日
回答済み: Walter Roberson 2015 年 7 月 15 日
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 Center および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by