Borrar filtros
Borrar filtros

loop and mixing solution

2 visualizaciones (últimos 30 días)
addy fang
addy fang el 27 de Jul. de 2020
Comentada: addy fang el 27 de Jul. de 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.

Respuesta aceptada

KSSV
KSSV el 27 de Jul. de 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 comentario
addy fang
addy fang el 27 de Jul. de 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)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Physics en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by