How to multiply different size arrays and multiply each element by each element?

51 views (last 30 days)
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

Alex
Alex about 2 hours ago
Edited: Matt J about 2 hours ago
I did some searching and installed deep learning theory:
What does this do and would it work for the above?
% your arrays
A = rand(1,4);
B = rand(1,6);
C = rand(1,8);
D = rand(1,2);
% get 4xN matrix of all combinations of A,B,C,D
inputs = combvec(A,B,C,D)';
% get a 1XN vector of results
res = inputs(:,1).*inputs(:,2).*inputs(:,3).*inputs(:,4);
I believe this worked! I got loads of results so it looks like it did!
  1 Comment
Torsten
Torsten 3 minutes ago
Edited: Torsten 2 minutes ago
I don't know if this is necessary for your application, but it might cost some effort to deduce which indices of A, B, C and D led to certain values of "res".

Sign in to comment.

More Answers (4)

Steven Lord
Steven Lord about 1 hour ago
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)
C = 24×3 table
Span Ct MAC ____ __ ___ 1 3 6 1 3 7 1 3 8 1 3 9 1 4 6 1 4 7 1 4 8 1 4 9 1 5 6 1 5 7 1 5 8 1 5 9 2 3 6 2 3 7 2 3 8 2 3 9
computation = C.Span.^2 + C.Ct.^3 - C.MAC.^4
computation = 24×1
-1268 -2373 -4068 -6533 -1231 -2336 -4031 -6496 -1170 -2275 -3970 -6435 -1265 -2370 -4065
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
We can spot check:
values = C{17, :} % Row 17; Span is 2, Ct is 4, MAC is 6
values = 1×3
2 4 6
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
calculationUsingIndividualValues = values(1).^2+values(2).^3-values(3).^4
calculationUsingIndividualValues = -1228
calculationsUsingCombinationsOutput = computation(17)
calculationsUsingCombinationsOutput = -1228

Torsten
Torsten on 16 Oct 2025 at 10:12
Moved: Torsten on 16 Oct 2025 at 10:12
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

Walter Roberson
Walter Roberson about 1 hour ago
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.

Matt J
Matt J about 2 hours ago
Edited: Matt J about 1 hour ago
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);

Products

Community Treasure Hunt

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

Start Hunting!