Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I vectorize this code?

First of all I should say that I couldn't find the appropriate title for my question so I would appreciate anyone who will edit the title!
Suppose that I have a 18432x1472 matrix and I want to convert it to a 3072x1472 one ( 18432/6 = 3072 ) in this form:
the mean of elements (1,6),(2,6),...,(6,6) of the old matrix will go to the element (1,1) of the new one the mean of elements (7,6),(8,6),...,(12,6) of the old matrix will go to the element (2,1) of the new one and so on
Up to now I have written this code:

function Out = MultiLooking( In )
   MatrixIn = double(In);
   m = size(In,1);
   InTranspose = MatrixIn';
   A = zeros(m,m/6);
   for i = 1:(m/6)
       A(6*(i-1)+1,i) = 1;
       A(6*(i-1)+2,i) = 1;
       A(6*(i-1)+3,i) = 1;
       A(6*(i-1)+4,i) = 1;
       A(6*(i-1)+5,i) = 1;
       A(6*(i-1)+6,i) = 1;
   end
   X = (InTranspose*A)/6;
   Out1 = X';
   Out = uint8(Out1);
end

But it is alittle slow and for my polarimetric SAR data, computer gets hanged out for a while when running this code so I need the code to run faster!
Can anyone suggest me a faster code for doing this purpose???

like image 237
Sepideh Abadpour Avatar asked Feb 06 '26 11:02

Sepideh Abadpour


1 Answers

An alternative to Divakar's nice answer: use blockproc (Image Processing Toolbox):

blockproc(MatrixIn, [6 size(MatrixIn,2)], @(x) mean(x.data))
like image 200
Luis Mendo Avatar answered Feb 07 '26 23:02

Luis Mendo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!