Afficient way to create "sum matrix"

1 visualización (últimos 30 días)
Orr Streicher
Orr Streicher el 1 de Feb. de 2021
Editada: Orr Streicher el 1 de Feb. de 2021
Hi,
So i would like to implement a function that get a vector as input lets :
g=[g1, g2, g3]
and return a matrix that contain a sum of pairs: so for g , the output matrix G contain 9 elements the G(i,j)=g(i)+g(j)
The problem is that g will be very large vector.
Does anyone know an efficient way to do that with sparse matrices
Thanks

Respuesta aceptada

Stephen23
Stephen23 el 1 de Feb. de 2021
g = 1:3;
m = g+g(:)
m = 3×3
2 3 4 3 4 5 4 5 6

Más respuestas (1)

Orr Streicher
Orr Streicher el 1 de Feb. de 2021
Hi,
So its looks like i was happy too soon =P
Since my vector g is very large ( vector in size of 65536)
I got memrory error ("Error using + Requested 65536x65536 (64.0GB)...)
Maybe there is other solution?
  2 comentarios
Stephen23
Stephen23 el 1 de Feb. de 2021
Editada: Stephen23 el 1 de Feb. de 2021
You could create a function, e.g.:
g = 1:3
g = 1×3
1 2 3
f = @(r,c)sum(g([r,c]));
f(1,2)
ans = 3
Your question mentions sparse matrices: are most of the values in g zero?
Orr Streicher
Orr Streicher el 1 de Feb. de 2021
Editada: Orr Streicher el 1 de Feb. de 2021
Hi,
In my case g doent have to be sparse (most of the values are not zero)
In addition i need the matrix form of G (for follwing calculations)

Iniciar sesión para comentar.

Categorías

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

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by