Azzera filtri
Azzera filtri

loop and mixing solution

2 visualizzazioni (ultimi 30 giorni)
addy fang
addy fang il 27 Lug 2020
Commentato: addy fang il 27 Lug 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.

Risposta accettata

KSSV
KSSV il 27 Lug 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 Commento
addy fang
addy fang il 27 Lug 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)

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Physics in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by