Write a MATLAB script to find the multiplies of the even numbers of any 𝑁 × 𝑀 matrix entered by the user?

2 次查看(过去 30 天)
clear all
clc
x=input('enter N x M marix ' );
a=rem(x,2);
b=~a;
z=~b ;
d=b .* x;
k=d + z;
g=prod(prod(k));
disp(['The multiplies of even number is equal to ' num2str(g)]);
  4 个评论
Walter Roberson
Walter Roberson 2022-2-26
That might be the homework question, but what is your question for us? You have posted some code. Are you encountering an error with the code?
Hussein Saeed
Hussein Saeed 2022-2-27
Yes, it’s my homework. The code went To multiple of even number of any Matrix okay I did my best . But when i add an odd matrix like [1 3 5] the answer is 1 but the correct answer is 0. I couldn’t solve this problem.

请先登录,再进行评论。

回答(2 个)

Walter Roberson
Walter Roberson 2022-2-27
x = [1 3 5]
x = 1×3
1 3 5
a=rem(x,2)
a = 1×3
1 1 1
okay, a is non-zero for each value that is not even and 0 for each even value
b=~a
b = 1×3 logical array
0 0 0
Okay, b is 0 for each value that is not even, and 1 for each even value
z=~b
z = 1×3 logical array
1 1 1
Okay, z is 1 for each odd value that is not even, and 0 for each even value. This differs from a in that a is the actual mod 2 values, which could for example be 0.3, but z is exactly 1 for those locations.
d=b .* x
d = 1×3
0 0 0
Recall that b is 0 for each value that is not even, and 1 for each even value. When we multiply by the original values, we zero out the values that are not even, and any even value is the same as the original value in that position
k=d + z
k = 1×3
1 1 1
So d is 0 for non-even values, and the original value for even values. And z is 1 for each non-even value, and 0 for each even value. It is not possible for both d and z to be 0 in any given position. So effectively you are replacing non-even values by 1, and even values are left alone. This is naively a decent strategy: provided that there is at least one even value, those 1's at the non-even locations will not disturb the product of the even values.
But... if there are no even values, then your entire matrix is 1, and the product of those is 1.
g=prod(prod(k))
g = 1
but the correct answer is 0
But is that true? What is the product of the empty set of numbers?
Consider the list [2,2,2] . The list is length 3, and the product is 2^3 = 8.
Consider the list [2,2] . The list is length 2, and the product is 2^2 = 4.
Consider the list [2]. The list is length 1, and the product is 2^1 = 1.
Now consider the list [] . The list is length 0, and the product is 2^0.
Now, what is 2^0 ? It is zero 2's multiplied together. Your thesis is that the value should be 0.
So 2^0 = 0? But 2^3 = 2 * 2^2, right? And 2^2 is 2 * 2^1, right? And 2^1 is 2 * 2^0 ? But if 2^0 is 0 then 2^1 = 2*2^0 ?= 2*0 --> 0. And then as you climb back up to 2^3 you would continue to get 0's.
We have now arrived at the point where either you have to special-case 2^0 in every expression like 2^(a+b), or else you have to suspect that defining 2^0, the product of the empty list, as being 0, is incorrect... that 2^0 should be 1, and so the product of the empty list should be 1.
Likewise, what is 3! ? By definition it is 3*2! . What is 2! ? By definition it is 2*1! . What is 1!? By definition it is... what? Just defined arbitrarily, or is it 1 * 0!? Because 0! is the product of the empty list so if the product of the empty list is 0 then you have to special case 0! in all probability expressions. For example, what is the number of combinations of n objects taken m at a time? n! / (m! * (n-m)! ) right? Now combinations of 3 objects taken 3 at a time is 3! / (3! * (3-3)!) = 1/0! . And since 0! is the product of the empty list, if you define the product of the empty list as 0 then you would be saying that the combinations would be 1/0 --> infinity ... or else you would have to define the combinations as:
n == m --> 1, else n!/(m!*(n-m)!)
... special casing what would otherwise be 0! in each place.
  1 个评论
Walter Roberson
Walter Roberson 2022-2-27
Consider
(x1*x2*x3... xn)
exp(log(x1*x2*x3... xn))
exp(log(x1)+log(x2)+log(x3)...)
exp(sum(log(x))
Let x be empty. log(x) is empty. Sum of empty is surely 0. exp(0) = 1. Therefore the product of empty x is 1.
If you wish to say that the product of the empty x is 0 then it would have to follow that the sum of empty is -infinity. Does that sound reasonable?

请先登录,再进行评论。


Abolfazl Chaman Motlagh
Matrix = randi(100,7,5)
Matrix = 7×5
21 9 40 3 14 92 80 69 3 62 65 38 57 13 40 30 10 51 16 71 86 46 34 26 90 51 24 6 99 71 34 27 45 93 93
multip = prod(Matrix(~rem(Matrix,2)))
multip = 2.8730e+27
  2 个评论
Walter Roberson
Walter Roberson 2022-2-27
Note that this was obviously a homework question, and you have just given away a complete solution instead of guiding the poster on how to find the solution themselves.
Abolfazl Chaman Motlagh
You're right. but there was some conditions about this particular question, due to that i answer.
  • he already had something to start solving the problem. i just present faster and cleaner method.
  • the question isn't that complex to say no helping allowed. probably it's not even the entire question. and he could find the answer just by browsing the web.
  • as someone who been TA for long time and for many times, if he could explain what this one line code is doing, he deserve the points for this kind of questions.
but still in general maybe that wasn't best i could do. thanks for reminding me.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Just for fun 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by