for文・if文を用いて、条件を満たすパターンの組み合わせを出したい
Afficher commentaires plus anciens
MATLAB初心者で手も足も出ないので、教えてください。
【やりたいこと】
条件を満たすような5つの変数のパターンの組み合わせを出したい。
任意の値:N=〇
変数:Za、Zb、Zc、Zd、Ze (全て整数で、値は5~150の範囲)
条件:Zc/Za>1
(Zb・Ze)/(Zc・Zd)>1
(Zb・Ze)/(Za・Zd)>1
X=(Za+Zc)/N (整数)
Y=(Za・Zd+Zb・Ze)/(N・(ZbとZdの最大公約数)) (整数)
Z=(Zc・ZdーZb・Ze)/(N・(ZbとZdの最大公約数)) (整数)
出力イメージとしては、
(Za、Zb、Zc、Zd、Ze)=(11、11、34、12、38)
(10、32、30、74、72)
・
・
・
よろしくお願いします。
2 commentaires
Hernia Baby
le 16 Déc 2022
Zaなどはそれぞれベクトルですか?また、条件を満たすときにどのような操作をするのでしょうか?
実香
le 16 Déc 2022
Réponse acceptée
Plus de réponses (1)
まずはデータを用意
N = 1;
Z = randi([5 150],5000,5);
Za = Z(:,1);
Zb = Z(:,2);
Zc = Z(:,3);
Zd = Z(:,4);
Ze = Z(:,5);
条件を作る
idx1 = Zc./Za > 1;
idx2 = (Zb.*Ze)./(Zc.*Zd)>1;
idx3 = (Zb.*Ze)./(Za.*Zd)>1;
X = (Za+Zc)./N;
Y = (Za.*Zd+Zb.*Ze)./(N.*gcd(Zb,Zd));
Z = (Zc.*Zd-Zb.*Ze)./(N.*gcd(Zb,Zd));
% 整数か判定
idx4 = X == floor(X);
idx5 = Y == floor(Z);
idx6 = Z == floor(Z);
全てを満たす条件
idx = idx1 & idx2 & idx3 & idx4 & idx5 & idx6;
条件に合うものを抽出
Z(idx,:)
3 commentaires
実香
le 16 Déc 2022
存在しないのはそのようなZa,Zb,Zc,Zd,Zeがここには存在しないからです。
そして新しいコメントをみたのですが、ここでいう組み合わせとは
a = (1:3)';
b = (1:3)';
C = [a,b]
でいうと
for ii = 1:height(C)
a(ii)*b(ii) > 5
end
のように3回テストをまわすのではなく、
for ii = 1:length(a)
for jj = 1:length(b)
a(ii)*b(jj) > 5
end
end
のように3×3=9回分テストを回すということなのでしょうか?
実香
le 19 Déc 2022
Catégories
En savoir plus sur General PDEs dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
