element wise sum in array

122 Ansichten (letzte 30 Tage)
Majbah
Majbah am 28 Mär. 2014
Beantwortet: dpb am 28 Mär. 2014
Hello,
I have a formula to calculate value, but, the value of elements will come from some elements of array. Like:
s_k=0.6 * t * x^3 * c^-4
t = [20 30 22 32 10 15 12 14 15 20];
x = [45 34 15 30 22 25 17 20 14 25];
c= [10 10 10 10 10 10 10 10 10 10];
p=[1 3 5 7];
I need the value of sum(s_k(p)).
How can I get that?
Thanks in advance.

Akzeptierte Antwort

Marta Salas
Marta Salas am 28 Mär. 2014
Bearbeitet: Marta Salas am 28 Mär. 2014
You have to use a dot in front of the operation: .* .^ to mean element-wise operation in matlab
t = [20 30 22 32 10 15 12 14 15 20];
x = [45 34 15 30 22 25 17 20 14 25];
c= [10 10 10 10 10 10 10 10 10 10];
s_k=0.6 * t .* x.^3 .* c.^-4
p=[1 3 5 7];
sum(s_k(p));

Weitere Antworten (1)

dpb
dpb am 28 Mär. 2014
Good spot for function handle...
>> f=@(t,x,c) 0.6.*t.*x.^3./c.^4
f =
@(t,x,c)0.6.*t.*x.^3./c.^4
>> f(t(p),x(p),c(p))
ans =
109.3500 4.4550 6.3888 3.5374
>> sum(f(t(p),x(p),c(p)))
ans =
123.7312
>>

Kategorien

Mehr zu Matrices and 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