Greetings again,
I have two arrays:
x = [1.3, 5.6, 7.8, -6.5, 0.3]
y = [134.2, 8786.98, -22.4, 4553518.6, 0.0019]
Now, I know how to do the dot product
a=dot(x,y)
and the reverse (i.e, the first dot product has the summation from 1 to 5, but the reverse is 5 to 1),
so
xback = fliplr(x);
yback = fliplr(y);
b=dot(x,y)
but what about largest-to-smallest order dot produc? Is there anyway to organize that in MATLAB from the original arrays?
I didn't know if this was with the sort command.
Thanks!

 채택된 답변

Walter Roberson
Walter Roberson 2013년 9월 27일
편집: Walter Roberson 2013년 9월 27일

0 개 추천

xy = x.*y;
dotxy = sum(sort(xy(xy>0), 'descend')) + sum(sort(xy(xy<0)));

댓글 수: 1

Jesse
Jesse 2013년 9월 29일
Yes - that's what I was looking for. Very tight too!
Thanks Walter!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2013년 9월 27일
편집: Azzi Abdelmalek 2013년 9월 27일

0 개 추천

If you sort a or b or both the result will be different.
Maybe you want to do other thing. Clarify what you want to achieve
% dot(a,b) is the same then sum(a.*b)

댓글 수: 3

Azzi Abdelmalek
Azzi Abdelmalek 2013년 9월 27일
[Jesse commented]
Basically I want to add the positive number is order from largest to smallest, then add the negative numbers in order from most to least negative, then add the partial sums. I hope this clarifies what I am trying to achieve.
Your suggestions?
Thanks Azzi!
Azzi Abdelmalek
Azzi Abdelmalek 2013년 9월 27일
No this is not clear, illustrate with a numeric example
Jan
Jan 2013년 9월 27일
편집: Jan 2013년 9월 27일
What is your purpose of creating the partial sums at first? Do you want to increase the accuracy of the result? Then better use FEX: XSum: XSum(a .* b).

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

질문:

2013년 9월 27일

댓글:

2013년 9월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by