How to automate the sequence of code?
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
yashvin
il 14 Lug 2015
Risposto: Walter Roberson
il 15 Lug 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
0 Commenti
Risposta accettata
Walter Roberson
il 15 Lug 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
0 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Multidimensional Arrays in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!