フィルターのクリア

How to use for Loop and if statement to construct a Pythagorean triple function?

3 ビュー (過去 30 日間)
NIANNIAN
NIANNIAN 2014 年 11 月 13 日
コメント済み: NIANNIAN 2014 年 11 月 13 日
I am working on construct a Pythagorean triple functions with inputs (a,b,c). Function: a^2+b^2=c^2
I am confused when the Matlab reminds me of NOT ENOUGH INPUT ARGUMENT. Should I initialize a,b,c? Besides I need use loop and if statement to check a,b,and c are all positive.
This is what I have written:
function [ Pyth] = isPythag( a,b,c )
d =a.^2+b.^2-c.^2; [row,column]=size(d);
for i=1:row for j=1:column if (d(i,j)==0) pyth(i,j)=1; else pyth(i,j)=0; end end end end

採用された回答

Adam
Adam 2014 年 11 月 13 日
編集済み: Adam 2014 年 11 月 13 日
Is the aim of your function simply to determine whether a, b, and c form a Pythagorean triple?
If so then yes you need to intialise them and pass 3 arguments to your isPythag function...e.g.
isPythag( 3, 4, 5 ) % True
isPythag( 4, 5, 6 ) % False
You don't need a for loop though unless you intend to pass in vectors of a, b and c and even then you can vectorise rather than use a for loop to give multiple answers.
You also need to be consistent with capitalisation or not of variable names.
'pyth' is a different variable to the 'Pyth' output variable your function expects.
  1 件のコメント
NIANNIAN
NIANNIAN 2014 年 11 月 13 日
I sincerely appreciate that. You reminded me of how careless I was. Cool, I definitely will also try your method.

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

その他の回答 (1 件)

Manoj
Manoj 2014 年 11 月 13 日
You need to change pyth to Pyth.
  1 件のコメント
NIANNIAN
NIANNIAN 2014 年 11 月 13 日
Oh,gosh. That is exactly what was wrong. Thank you so much. My bad.

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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by