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

11 ビュー (過去 30 日間)
Alex
Alex 2025 年 10 月 16 日
編集済み: Torsten 2025 年 10 月 16 日
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.

採用された回答

Alex
Alex 2025 年 10 月 16 日
編集済み: Matt J 2025 年 10 月 16 日
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 件のコメント
Torsten
Torsten 2025 年 10 月 16 日
編集済み: Torsten 2025 年 10 月 16 日
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".

サインインしてコメントする。

その他の回答 (4 件)

Steven Lord
Steven Lord 2025 年 10 月 16 日
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 2025 年 10 月 16 日
移動済み: Torsten 2025 年 10 月 16 日
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 2025 年 10 月 16 日
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 2025 年 10 月 16 日
編集済み: Matt J 2025 年 10 月 16 日
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 件のコメント
Walter Roberson
Walter Roberson 2025 年 10 月 16 日
The * operations are not going to work between 3D arrays.
Matt J
Matt J 2025 年 10 月 16 日
Yep. Fixed it.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by