# Recording variables that satisfy a condition

5 ビュー (過去 30 日間)
Tyler Dabney 2021 年 1 月 28 日

Typical new matlab user here, so just looking for help and to learn, I know this is probably a simple question.
Basically, I have three variables X, Y, and Z that span from 0 to 1 spaced by 100. I want to record all instances where X + Y + Z = 1. This is what I have so far:
X = linspace(0,1,100);
Y = linspace(0,1,100);
Z = linspace(0,1,100);
poss = zeros(1000,3); %just a space to populate answers
for i = 1:100
for j = 1:100
for k = 1:100
p = X(i)+Y(j)+Z(k);
if p==1
poss(i,1)=X(i);
poss(i,2)=Y(j);
poss(i,3)=Z(k);
end
end
end
end
Am I on the right track? I just want to record the three variables X, Y, and Z in three columns if their sum totals to one. This is part of a more complex problem but I tried to simplify it. Thanks
##### 2 件のコメント1 件の古いコメントを表示1 件の古いコメントを非表示
madhan ravi 2021 年 1 月 28 日
Yes, thanks Stephen for the explanations.

サインインしてコメントする。

### 採用された回答

madhan ravi 2021 年 1 月 28 日
X = linspace(0,1,100);
Y = linspace(0,1,100);
Z = linspace(0,1,100);
[XX, YY, ZZ] = meshgrid(X, Y, Z);
XYZ = XX + YY + ZZ;
Pass(:, 3) = ZZ(XYZ == 1);
Pass(:, 2) = YY(XYZ == 1);
Pass(:, 1) = XX(XYZ == 1);
Pass
Pass = 4842×3
0 1.0000 0 0.0101 0.9899 0 0.0202 0.9798 0 0.0303 0.9697 0 0.0404 0.9596 0 0.0505 0.9495 0 0.0606 0.9394 0 0.0707 0.9293 0 0.0808 0.9192 0 0.0909 0.9091 0

サインインしてコメントする。

### その他の回答 (1 件)

David Hill 2021 年 1 月 28 日
[x,y,z]=meshgrid(X,Y,Z);
k=x+y+z;
idx=find(k==1);

サインインしてコメントする。

### カテゴリ

Help Center および File ExchangeLogical についてさらに検索

### Community Treasure Hunt

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

Start Hunting!