フィルターのクリア

Resolving the "Homeworks"... is it correct?

1 回表示 (過去 30 日間)
ricsf
ricsf 2015 年 2 月 4 日
編集済み: Andrew Newell 2015 年 2 月 4 日
Answer a) Write a Function in Matlab which receives a Matrix and a Int for input argument and returns the transformed Matrix, and in this transformed Matrix all of the Int divisible elements were devised by the Int.
b) Also write a Matlab script that allows and ask the user about the elements and dimensions of a Matrix, invoke the Function (a)) and the transformed Matrix is showned in the screen.
R: a)
function N = divide(A,b)
for i=1:size(A)
for j=1:size(A)
if mod(A(i,j),b)==0
A(i,j)=A(i,j)/b
else
A(i,j)=0
end
end
end
b)
d=input('dimension of the matrix')
for i=1:d
for j=1:d
M(i,j)=input('values')
end
end
q=input('value to use for split)
disp(divide(M,q))
  4 件のコメント
ricsf
ricsf 2015 年 2 月 4 日
no, only divide the divisible elements...
Jan
Jan 2015 年 2 月 4 日
Then omit the "else A(i,j)=0".

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

回答 (2 件)

Jan
Jan 2015 年 2 月 4 日
編集済み: Jan 2015 年 2 月 4 日
Did you try to run the code? Do you have any problems with running this code?
It looks fine.
  2 件のコメント
ricsf
ricsf 2015 年 2 月 4 日
No, I didn't! I'm doing this on paper :-)
Andrew Newell
Andrew Newell 2015 年 2 月 4 日
編集済み: Andrew Newell 2015 年 2 月 4 日
Your chances of getting it right without testing it are slim to nil. For example, it is not immediately obvious that the output variable N is never assigned a value. But in a test, you'll quickly see a problem!

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


Andrew Newell
Andrew Newell 2015 年 2 月 4 日
編集済み: Andrew Newell 2015 年 2 月 4 日
Instead of saying yes or no, I suggest you create some tests to see if it does what you want. Some examples:
A = eye(2);
divide(A,0)
divide(A,1)
divide(A,2)
What should you get in each case? What do you actually get?

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by