element by element sum with two arrays

11 ビュー (過去 30 日間)
Adam Parry
Adam Parry 2012 年 7 月 3 日
I basicly have to arrays A1 and B1 and would like to do this
sum(over i)((A1(i)-a*B1(i)-b)^2)
so i tried using this
sum((A1-a*B1-b).^2)
but that does not do it element by element
any hints?
a and b are constants
  1 件のコメント
Kye Taylor
Kye Taylor 2012 年 7 月 3 日
Can you provide concrete values for a,b,A1,B1 as @Thomas does above? Can you also give/describe the output that you're looking for?

サインインしてコメントする。

回答 (1 件)

Thomas
Thomas 2012 年 7 月 3 日
Is this how you want it..
A1=[1:4];
B1=[5:8];
a=2;
b=2;
c=(A1-a.*(B1-b)).^2 % just to show the element wise subtract and square
sum(c)
  2 件のコメント
Adam Parry
Adam Parry 2012 年 7 月 4 日
I don't think that is quite right.
C = (A1-(a*B1)-b).^2
but will it still give me the correct element wise sum?
Ryan
Ryan 2012 年 7 月 4 日
編集済み: Ryan 2012 年 7 月 4 日
Thomas did misplace a parentheses.
What is in your original post should do exactly what you asked it to do. By using "sum()" you are summing up all of the components of the vector thus resulting in a singular result. If that is not what you'd like, then you should try adding more detail and writing out your problem statement as clear as possible so we can try to help.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by