Calculating all combinations of vector-element multiplication
Ältere Kommentare anzeigen
I have N vectors of varying size for which I wish to compute the product of every element.
A = [1 2 3 4]
B = [1]
C = [10 20]
Desired result = [a1*b1*c1 a1*b1*c2 a2*b1*c1 a2*b1*c2...] --> size = 1 x 8
I have found a few post for the 2 vector case in which simple vector multiplication works
D = A'*B --> this results in a matrix, but the entries align with what I am trying to achieve.
Expanding this to a 3-vector problem I had some success with bsxfun
D = bsxfun(@times,A'*B,reshape(C,1,1,numel(C))); --> this results in a 3-D matrix
How do you do this for more than 3 vectors, lets say 10, without using embedded 'for' loops???
Akzeptierte Antwort
Weitere Antworten (1)
Sid Parida
am 5 Jun. 2018
Bearbeitet: Sid Parida
am 5 Jun. 2018
Not sure of internal tools but use the two files attached above (found on File Central) and use the following code:
a = [1 2 3 4]
b = [1]
c = [10 20]
D = prod(cartprod(a, b, c), 2)'
D should contain the desired result. It works for higher number of vectors too.
Kategorien
Mehr zu Matrix Indexing finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!