Outer products are faster with b*b' in matlab?

5 visualizaciones (últimos 30 días)
z cy
z cy el 23 de Sept. de 2024
Comentada: Bruno Luong el 23 de Sept. de 2024
I have a column vector with about 6800 elements, many of which are 0. How can I quickly calculate the multiplication of the column vector and its transpose?
a = rand(77,1);
b = [a; zeros(6800,1)]
tic
v = b*b';
% 0.053078s
toc
tic
v = bsxfun(@times, b, b.');
%0.066428s
toc
  5 comentarios
z cy
z cy el 23 de Sept. de 2024
Editada: z cy el 23 de Sept. de 2024
Thanks!
I have found the faster way as you shown!
Just use A*A', while A is a 6800*100 matrix, then I can get the final result.
Bruno Luong
Bruno Luong el 23 de Sept. de 2024
If you know A befre hand yes. You never state that in your question.

Iniciar sesión para comentar.

Respuesta aceptada

Sandeep Mishra
Sandeep Mishra el 23 de Sept. de 2024
Editada: Sandeep Mishra el 23 de Sept. de 2024
Hi Z,
From the code snippet, it's clear that you are aiming to calculate the product of a matrix with its transpose, where a significant portion of the matrix elements are zeros.
To improve efficiency, MATLAB’s ‘sparse’ matrix can be employed.
By converting the matrix into a sparse format, you can effectively reduce memory usage since sparse matrices only store non-zero elements. This can result in a faster computation, especially when dealing with large matrices where most elements are zero.
Refer to the following code snippet to implement the functionality:
a = rand(77,1);
b = [a; zeros(6800,1)];
% Converting matrix to sparse matrix
b_sparse = sparse(b);
tic
% Calculating Sparse product
v_sparse = b_sparse * b_sparse';
% Converting Sparse product result to Full matrix form
v_full = full(v_sparse);
toc
Elapsed time is 0.004505 seconds.
For more information, refer to the following MathWorks Documentation:
  1. ‘sparse’ function: https://www.mathworks.com/help/releases/R2022b/matlab/ref/sparse.html
  2. 'full' function: https://www.mathworks.com/help/releases/R2022b/matlab/ref/full.html
I hope this approach helps in addressing the issue effectively.

Más respuestas (0)

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by