Write a MATLAB script to find the multiplies of the even numbers of any 𝑁 × 𝑀 matrix entered by the user?
6 次查看(过去 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
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?
回答(2 个)
Walter Roberson
2022-2-27
x = [1 3 5]
a=rem(x,2)
okay, a is non-zero for each value that is not even and 0 for each even value
b=~a
Okay, b is 0 for each value that is not even, and 1 for each even value
z=~b
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
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
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))
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
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
2022-2-26
Matrix = randi(100,7,5)
multip = prod(Matrix(~rem(Matrix,2)))
2 个评论
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
2022-2-27
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.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!