Filter löschen
Filter löschen

loop and mixing solution

2 Ansichten (letzte 30 Tage)
addy fang
addy fang am 27 Jul. 2020
Kommentiert: addy fang am 27 Jul. 2020
Variables A, B, C each varies from 0 to 100, and their sum equal to 100.
How should I do it?
I was trying to run this:
for A, B, C = 0:10:100, A+B+C=100,
z=A*0.1+B*0.2+C*0.3
end
It did not work. Apparently.

Akzeptierte Antwort

KSSV
KSSV am 27 Jul. 2020
A = 0:10:100 ;
B = A ;
C = A ;
[A,B,C] = meshgrid(A,B,C) ;
% convert them to columns
A = A(:) ; B = B(:) ; C = C(:) ;
% get the sum
thesum = A(:)+B(:)+C(:) ;
% get the indices whos sum isequal to 100
idx = thesum==100 ;
% pick the values
A = A(idx) ; B = B(idx) ; C = C(idx) ;
% ge the required Z
Z = 0.1*A+0.2*B+0.3*C ;
  1 Kommentar
addy fang
addy fang am 27 Jul. 2020
Thank you. That works!
One more question, if I need to get z from a few tables, can I do the following?
If Variables A, B, C each varies from 0 to 100, and their sum equal to 100.
for A, B, C = 0:10:100, A+B+C=100,
z=A*T1.+B*T2.+C*T3.
end
T1, T2, T3 are tables, containing similar structures (same columns/rows, similar type of data)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Physics finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by