loop and mixing solution

2 次查看(过去 30 天)
addy fang
addy fang 2020-7-27
评论: addy fang 2020-7-27
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.

采纳的回答

KSSV
KSSV 2020-7-27
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 个评论
addy fang
addy fang 2020-7-27
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)

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Physics 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by