How to multiply different size arrays and multiply each element by each element?
Show older comments
I have an equation with 4 arrays in, all of a different size. I want every combination of the arrays possible and multiply all by all because i'm going to filter out the outputs later. It's almost like a simplex algorithm but in one equation with the limits set by the peson inputting the data. Thanks for all your help!
Alex
Vh = ((Span * ((1 - Ct)+1)*MAC/2) * CoM)/(WingArea*WChord);
% Where, Span, Ct, MAC, and CoM are all differently sized arrays.
Accepted Answer
More Answers (4)
I recommend using the combinations function to generate the combinations of values, then using variables from the table returned by combinations in your calculations.
Span = [1 2];
Ct = [3 4 5];
MAC = [6 7 8 9];
C = combinations(Span, Ct, MAC)
computation = C.Span.^2 + C.Ct.^3 - C.MAC.^4
We can spot check:
values = C{17, :} % Row 17; Span is 2, Ct is 4, MAC is 6
calculationUsingIndividualValues = values(1).^2+values(2).^3-values(3).^4
calculationsUsingCombinationsOutput = computation(17)
If nothing helps, make a 4-fold nested loop.
for i=1:numel(Span)
for j = 1:numel(MAC)
for k = 1:numel(CoM)
for l = 1:numel(WingArea)
result(i,j,k,l) = ...
end
end
end
end
1 Comment
Dyuman Joshi
on 16 Oct 2025
Also, best to pre-allocate the output variable.
Walter Roberson
on 16 Oct 2025
Vh = ((Span(:) .* ((1 - reshape(Ct,1,[]))+1).*reshape(MAC,1,1,[])/2) .* reshape(CoM,1,1,1,[]))./(WingArea.*WChord);
assuming that WingArea and WChord are scalars.
Download ndgridVecs from the File Exhange,
Span = [1 2];
Ct = [3 4 5];
MAC = [6 7 8 9];
[Span, Ct, MAC] = ndgridVecs(Span, Ct, MAC);
Vh = ((Span .* ((1 - Ct)+1)*MAC/2) .* CoM)./(WingArea.*WChord);
2 Comments
Walter Roberson
on 16 Oct 2025
The * operations are not going to work between 3D arrays.
Matt J
on 16 Oct 2025
Yep. Fixed it.
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!