multiply random elements of matrix

1 view (last 30 days)
Suresh Dahal
Suresh Dahal on 27 Aug 2017
Commented: John D'Errico on 28 Aug 2017
hi, I am stuck on something and I need your help guys. I have a number X and a column matrix with A=[25;12;13;19;23...]. Let's say I want to check if product of any two elements (randomly selected) of these matrix is equal to A or not, how am I supposed to do that? thanks in advance.
  3 Comments
Jan
Jan on 27 Aug 2017
Please mention, if this is a homework, because this would require a different kind of answers.
John D'Errico
John D'Errico on 27 Aug 2017
Be careful. Unless the elements of A are (sufficiently small) integers, testing for exact equality will probably cause failure.

Sign in to comment.

Answers (2)

Jan
Jan on 27 Aug 2017
Edited: Jan on 28 Aug 2017
Create a matrix, which contains the product of all elements with another element:
A = [25; 12; 13; 19; 23];
AA = A * A.';
Now you search if X is any of the elements of AA.
Note: A * A.' is not the auto-expanding, but the dyadic product, so it works in older Matlab versions also.
[EDITED] This is useful from small vectors only. For 1e6 elements this consumes a lot of memory. A*A' is symmetric, such that testing all elements is not efficient.

John D'Errico
John D'Errico on 27 Aug 2017
Edited: John D'Errico on 27 Aug 2017
Assuming that A contains only integers, you can use the brute force approach as Jan suggested. Or you can determine if any of the elements of A are proper divisors of the number X, then you need only test to see if X divided by that number is also a member of A.
The point is, this requires only a couple of ismember tests, as well as needing to write a tool to generate all proper divisors of a number X. That itself is not difficult, given the factors of X. On the other hand, if X is too large, factoring it might itself be a difficult problem.
Is this a better algorithm than the brute force search that generates all possible products? Yes, IF your vector is large. So if the vector had 100000 elements in it, generating the outer product matrix will be quite CPU and memory intensive.
The point is many algorithms are optimal in one regime, but will fail miserably in other regimes for your data. You need to understand the given algorithm and its requirements.
  2 Comments
Jan
Jan on 28 Aug 2017
+1: Searching for X ./ A in A needs less operations than creating A * A'.
John D'Errico
John D'Errico on 28 Aug 2017
Yet, this algorithm is more complicated than the brute force outer product and search. If A has length 10 or 100, then it would probably be silly to write sophisticated code. Don't write complicated algorithms when there is no need for them. Now, I'm not saying that the algorithm I proposed is overly complex. But the advice is still appropriate in general. Sometimes you need to write a hybrid algorithm, that does the simple solution for small problems, yet switches over for big ones.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!