Array Cascade Addition calculation

조회 수: 2 (최근 30일)
Andrew Tubbs
Andrew Tubbs 2021년 1월 22일
댓글: Steven Lord 2021년 1월 23일
I have several arrays and what I need to do is create a loop that calculates the values of the adition of each value of an array with each value of another array as show below:
Array:
A = [A1 A2 A3] B = [B1 B2 B3] C = [C1 C2 C3]
Needed Values:
D = A1+B1+C1 E = A1+B1+C2 F = A1+B1+C3 G = A1+B2+C1 H = A1+B2+C2 I = A1+B2+C3 .... and so on
On a side note, what is the MATLAB notation for a specific value of an array such as A(1) = A1

채택된 답변

James Tursa
James Tursa 2021년 1월 22일
편집: James Tursa 2021년 1월 22일
One way using implicit array expansion:
D = A(:) + reshape(B,1,[]) + reshape(C,1,1,[]);
Your results are the elements of D.
I'm not sure what you are asking about A(1) vs A1. A(1) is the MATLAB notation for the 1st element of variable A. A1 would be the name of a different variable ... it has no relationship to variable A.
  댓글 수: 2
Andrew Tubbs
Andrew Tubbs 2021년 1월 23일
What I get with this is jumbles of information, where only the first value is correct. To be more specific each value of each array is an 11 value array itself and I need a loop that automatically that spits out the summation array.
Steven Lord
Steven Lord 2021년 1월 23일
A = [1 2 4];
B = [8 16 32];
C = [64 128 256];
D = reshape(A, 3, 1) + reshape(B, 1, 3) + reshape(C, 1, 1, 3)
D =
D(:,:,1) = 73 81 97 74 82 98 76 84 100 D(:,:,2) = 137 145 161 138 146 162 140 148 164 D(:,:,3) = 265 273 289 266 274 290 268 276 292
[A(2)+B(3)+C(1), D(2, 3, 1)]
ans = 1×2
98 98

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by