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

조회 수: 54 (최근 30일)
Alex
Alex 2025년 10월 16일 9:46
편집: Torsten 2025년 10월 16일 19:17
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일 17:06
편집: Matt J 2025년 10월 16일 17:30
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일 19:17
편집: Torsten 2025년 10월 16일 19:17
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일 13:52
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일 10:12
이동: Torsten 2025년 10월 16일 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 2025년 10월 16일 16:14
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일 16:48
편집: Matt J 2025년 10월 16일 17:31
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);

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by