Most efficient way to sum anti-diagonal elements

조회 수: 21 (최근 30일)
Jon Paul Janet
Jon Paul Janet 2016년 7월 10일
댓글: Hongbo Zhao 2018년 10월 10일
Inside an ODE that I am solving (ode15s), I need to do the following. Let the state vector by y (Nx1), and for some fixed, sparse symmetric A (NxN), I need to have that
y' = [sum of anti-diagonal elements of (diag(y)*A*diag(y))] + f(t)
for some forcing f. The problem is, for large N (10k + ) this is pretty slow as the solver takes many steps. Currently I have calculated a logical index mask (outside the ode) that gives me the elements I want, and my code (inside the d.e.) is:
A = spdiags(y,0,N,N)*A*spdiags(y,0,N,N); %overwrite A in-place with y*A*y
working_matrix=sparse([],[],[],2*N-1,N,N*ceil(N/2)); % sparse allocation
working_matrix(index_map)=A; %strip antidiagonals to columns
this gives me working_matrix, which has the antidiagonal elements of (diag(y)*A*diag(y) which I can just sum over. However, 99% of my runtime is spent on the line "working_matrix(index_map)=A". Any speedup on this line would save me a lot of time. "index_map" is a (2*N-1)xN logical array that pulls out the correct elements, based on this work here.
Is there a better way? I can pull of the antidiagonal elements of A outside the solver and pass the matrix that has the antidiagonal elements as rows, but then I still need the same construction applied to y*y' to get the matching elements of y - unless there is a better way to do this?
I am running R2015b if that matters.

채택된 답변

Andrei Bobrov
Andrei Bobrov 2016년 7월 12일
"all of the elements on any NE-SW diagonal"
a = randi(50,6);
[m,n] = size(a);
idx = hankel(1:m,m:(n-1)+m);
out = accumarray(idx(:),a(:));
  댓글 수: 4
Salvatore Russo
Salvatore Russo 2018년 5월 3일
thank you very much sir.... very elegant solution
Hongbo Zhao
Hongbo Zhao 2018년 10월 10일
Very elegant indeed.

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

추가 답변 (3개)

Walter Roberson
Walter Roberson 2016년 7월 11일
[r, c] = size(A);
sum(A(r : r-1 : end))
  댓글 수: 2
Stephen23
Stephen23 2016년 7월 12일
편집: Stephen23 2016년 7월 12일
+1 for a very neat solution. Note the indices must end with end-1:
>> X = [1,2,3;4,5,6;7,8,9]
X =
1 2 3
4 5 6
7 8 9
>> [r,c] = size(X);
>> X(r:r-1:end-1)
ans =
7 5 3
>> X(r:r-1:end)
ans =
7 5 3 9
The code should be:
>> sum(X(r:r-1:end-1))
ans = 15
Also note that this will only work for square matrices (and a few other cases) but not for any general rectangular matrix.
Walter Roberson
Walter Roberson 2016년 7월 12일
For non-square matrices,
sum( X(r:r-1:r*(r-1)+1) )

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


Thorsten
Thorsten 2016년 7월 11일
You could try
S = sum(sum(A - diag(diag(A))));
  댓글 수: 1
Jon Paul Janet
Jon Paul Janet 2016년 7월 11일
I'm sorry if I was not clear, but the anti-diagonal elements are those on the NE-SW diagonal, like this, except generalized in the same way as diag(A,1) to diag(A,N/2). In other words, all of the elements on any NE-SW diagonal

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


Sean de Wolski
Sean de Wolski 2016년 7월 11일
  댓글 수: 1
Walter Roberson
Walter Roberson 2016년 7월 12일
I don't think this is going to be the most efficient...

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

카테고리

Help CenterFile Exchange에서 Operating on Diagonal Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by