Multiply every column of matrix X by another vector V
DataX = 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;
Post Matlab code snippets. Sort by tags, people, people and tags, etc.. Brought to you by XTargets - Consulting & Application devlopment.
Multiply every column of matrix X by another vector VDataX = 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 indiciesVectorize thisfor 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'skeepers = (x>0); colSums = sum(x .* keepers); counts = sum(keepers); means = colSums ./ counts; Reverse one column or row of a matrixTo reverse column or row c of a Matrix MM(:,c)=flipud(M(:,c)) M(c,:)=flipud(M(c,:))
|
|