필터 지우기
필터 지우기

sum within the elements of a vector

조회 수: 2 (최근 30일)
pavlos
pavlos 2012년 2월 29일
Hello,
Suppose you have the following 1xn vector:
v=[(1,1),(1,2),...,(1,n)]
How the sum of the elements (each other) is calculated?
For example,
The sum of first element with the others
{[(1,1)+(1,2)} {(1,1)+(1,3)} {(1,1)+(1,4)} . . . {(1,1)+(1,n)}
The sum of second element with the others
{(1,2)+(1,3)} {(1,3)+(1,4)} . . . {(1,2)+(1,n)}
and so on.
The sum {(1,1)+(1,2)} is the same with the {(1,2)+(1,1)} and should only considered once.
Also the sum of an element with itself, shouldn`t be taken into account.
Thank you.
Best,
Pavlos

채택된 답변

Oleg Komarov
Oleg Komarov 2012년 2월 29일
v = 1:10;
out = bsxfun(@plus,v,v');
EDIT make diagonal elements = 0
[r,c] = size(out);
out(1:r+1:r*c) = 0;
  댓글 수: 1
pavlos
pavlos 2012년 2월 29일
Thank you for your response.
My mistake: The sum between {(1,1)+(1,2)} and {(1,2)+(1,1)} should be considered,
while the sum of an element with itself should not .
That means that only the main diagonal of the matrix has zeros.
Any help please?
Thank you.
Best,
Pavlos

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

추가 답변 (1개)

Sean de Wolski
Sean de Wolski 2012년 2월 29일
Edit
x = 1:5;
M = bsxfun(@plus,x',x);
M(1:size(M,1)+1:end) = 0;
  댓글 수: 1
pavlos
pavlos 2012년 2월 29일
That worked fine.
Thank you.
Best,
Pavlos

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

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by