How to avoid loops for the following matrix manipulcation

I need to do the following operations on a series of matrix A1,A2,...,A_m\in\reals^{n\times n}, and two vectors c,x\in\reals^{n}.
  • H\in\reals^{n\times n} = \sum_{i=1}{m}(A_i*x)(A_i*x)^T,
  • b\in\reals^{n} = \sum_{i=1}{m}c_i*(A_i*x).
I guess no-loop implementation will be faster. However, I tried and failed. Any ideas, guys?
Appreciate your time in advance!

8 Commenti

btw, A_i is sparse if that matters here.
Are you sure the calculation isn't (A_i*x)^T * (A_i*x) ?
Stephen23
Stephen23 il 25 Gen 2018
Modificato: Stephen23 il 25 Gen 2018
"I need to do the following operations on a series of matrix A1,A2,...,A_m..."
"I guess no-loop implementation will be faster."
Accessing numbered variables in a loop is slow. It is not fast.
You could change your data design to use one array, in which case you could write fast code.
@James Tursa yeah (A_i*x)(A_i*x)^T is a matrix while (A_i*x)^T * (A_i*x) is a scale. The former is what i need.
Chuangchuang Sun comments to me:
I am not naming them dynamically. I stack them in one matrix, or in a structure. does that make a difference?
Stephen23
Stephen23 il 26 Gen 2018
Modificato: Stephen23 il 26 Gen 2018
Chuangchuang Sun's "Flag" moved here:
n is around 100

Accedi per commentare.

Risposte (1)

James Tursa
James Tursa il 25 Gen 2018
Modificato: James Tursa il 25 Gen 2018
Since the A_i are sparse, you are probably stuck with your loop. Consider storing them as A{i} instead of A_i, however, to make looping easier. And code it such that you do the A{i}*x calculation only once.

Categorie

Richiesto:

il 25 Gen 2018

Modificato:

il 26 Gen 2018

Community Treasure Hunt

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

Start Hunting!

Translated by