-1

Can anyone please help me write this code for all 'ht' in matlab?. Thank you.

ytop=[0 0 2.4 5.0 5.0 5.0 5.0 5.0 5.0 5.0]
  ybot=[0 -2.4 -3.9 -4.7 -4.9 -4.7 -3.8 -2.3 0.1 5.0]
  ht=((ytop(2)-ybot(2))+(ytop(1)-ybot(1)))/2

2 Answers2

1

If you are asking how to do ((ytop(i)-ybot(i))+(ytop(i-1)-ybot(i-1)))/2 for all i then just use element-wise operations and a bit of indexing:

((ytop(2:end) - ybot(2:end))./((ytop(1:end-1) - ybot(1:end-1)))/2
Dan
  • 45,079
  • 17
  • 88
  • 157
0

If you mean you need calculate ht for each index you can go this way:

fun = @(x) ((ytop(x)-ybot(x))+(ytop(x-1)-ybot(x-1)))/2
ind = 2:10
result = arrayfun(fun, ind)

and get this result:

result =
1.2000    4.3500    8.0000    9.8000    9.8000    9.2500    8.0500    6.1000    2.4500
Mikhail_Sam
  • 10,602
  • 11
  • 66
  • 102