[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: vectorise matrix slice extraction
From: |
Francesco Potorti` |
Subject: |
Re: vectorise matrix slice extraction |
Date: |
Sat, 28 Feb 2009 02:30:05 +0100 |
>> Is there a way to efficiently remove the for loop in this assignment?
>>
>> for jj = length(pple):-1:1
>> llk(:,:,:,jj) = ll(:,:,:,pple(jj),tple(jj));
>> endfor
>
>n = length (pple);
>i = sub2ind(size (ll) (4:5), pple, tple);
>llk(:,:,:,1:n) = ll(:,:,:,i);
Yes, this is it! No reshape, no temp variables. I had never realised
that you can access a 5-d matrix with four indices. It is even simpler
than the above:
jj = sub2ind(size(ll)(4:5), pple, tple);
llk = ll(:,:,:,jj);
It is slightly faster than the for loop, maybe more elegant, maybe less
clear.
Thanks
--
Francesco Potortì (ricercatore) Voice: +39 050 315 3058 (op.2111)
ISTI - Area della ricerca CNR Fax: +39 050 315 2040
via G. Moruzzi 1, I-56124 Pisa Email: address@hidden
(entrance 20, 1st floor, room C71) Web: http://fly.isti.cnr.it/