Page 1 of 1

Creating a better version of the diag function

PostPosted: Fri Dec 25, 2009 10:10 am
by linda
Hi,
I need to extract the main diagonal of a matrix with lower complexity than the "diag" function has. I tried the following simple loop:

for i=1:length(a)
my_diag(i)=a(length(a)*(i-1)+i)
end

Unsurprisingly it works, but it's much slower than "diag".
Any ideas?

Thanks