How to make up the diagonal summation without for loop and with the fastest way!
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
HYUNCHUL
am 1 Apr. 2014
Kommentiert: Andrei Bobrov
am 1 Apr. 2014
A =
1 7 13
2 8 14
3 9 15
4 10 16
5 11 17
6 12 18
B =
1, (2+7), (3+8+13), (4+9+14), (5+10+15) ... (12+17), 18
How to make the B vector?
Vector-wise would be good to perform the enhancement of computational time?
0 Kommentare
Akzeptierte Antwort
Andrei Bobrov
am 1 Apr. 2014
A =[ 1 7 13
2 8 14
3 9 15
4 10 16
5 11 17
6 12 18];
B = sum(spdiags(rot90(A)));
2 Kommentare
Weitere Antworten (3)
Azzi Abdelmalek
am 1 Apr. 2014
Bearbeitet: Azzi Abdelmalek
am 1 Apr. 2014
A =[ 1 7 13
2 8 14
3 9 15
4 10 16
5 11 17
6 12 18]
[n,m]=size(A);
B=fliplr(A);
out=zeros(1,m+n-1);
for k=-n+1:m-1
out(k+n)=sum(diag(B,k));
end
out=fliplr(out)
0 Kommentare
Mischa Kim
am 1 Apr. 2014
Bearbeitet: Mischa Kim
am 1 Apr. 2014
Hyunchul, something like
fA = fliplr(A);
B = fliplr(arrayfun(@(i) sum(diag(fA,i)),-length(A(:,1))+1:length(A(1,:))-1));
0 Kommentare
Sean de Wolski
am 1 Apr. 2014
And just for fun (requires Image Processing):
B = zeros(size(A));
B(1) = 1;
B2 = bwdist(B,'cityblock')+1;
v = accumarray(B2(:),A(:))
Use Azzi's solution.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!