Write a MATLAB script to find the multiplies of the even numbers of any 饾憗 脳 饾憖 matrix entered by the user?

13 views (last 30 days)
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 Comments
Hussein Saeed
Hussein Saeed on 27 Feb 2022
Yes, it鈥檚 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鈥檛 solve this problem.

Sign in to comment.

Answers (2)

Walter Roberson
Walter Roberson on 27 Feb 2022
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 Comment
Walter Roberson
Walter Roberson on 27 Feb 2022
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?

Sign in to comment.


Abolfazl Chaman Motlagh
Abolfazl Chaman Motlagh on 26 Feb 2022
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 Comments
Abolfazl Chaman Motlagh
Abolfazl Chaman Motlagh on 27 Feb 2022
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.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by