« Earlier4 items total Later »

Multiply every column of matrix X by another vector V

Data
X = rand(1000,500);
V = rand(1,1000);

Non vectorized
X2 = zeros(1000,500)
for k = 1:500
   X2(:,k) = V.*X(:,k);
end


Fastest method

X2 = zeros(1000,500)
X2 = diag(sparse(V))*X;

Vectorize operations based on column and row indicies

Vectorize this
for i = 1:100
  for j = 1:100
    r(i,j) = sqrt(i^2+j^2);
  end
end


becomes

[i,j]=meshgrid(1:100,1:100);
r = sqrt(i.^2+j.^2);

Obtain the mean of each column ignoring 0's

keepers = (x>0);
colSums = sum(x .* keepers);
counts = sum(keepers);
means = colSums ./ counts;

Reverse one column or row of a matrix

To reverse column or row c of a Matrix M
M(:,c)=flipud(M(:,c))


M(c,:)=flipud(M(c,:))

« Earlier4 items total Later »




Sponsored by

Sole Central

Your one stop shop for Birkenstock and Crocs shoes and sandles.