필터 지우기
필터 지우기

multiply random elements of matrix

조회 수: 3 (최근 30일)
Suresh Dahal
Suresh Dahal 2017년 8월 27일
댓글: John D'Errico 2017년 8월 28일
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
Jan
Jan 2017년 8월 27일
Please mention, if this is a homework, because this would require a different kind of answers.
John D'Errico
John D'Errico 2017년 8월 27일
Be careful. Unless the elements of A are (sufficiently small) integers, testing for exact equality will probably cause failure.

댓글을 달려면 로그인하십시오.

답변 (2개)

Jan
Jan 2017년 8월 27일
편집: Jan 2017년 8월 28일
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 2017년 8월 27일
편집: John D'Errico 2017년 8월 27일
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
Jan
Jan 2017년 8월 28일
+1: Searching for X ./ A in A needs less operations than creating A * A'.
John D'Errico
John D'Errico 2017년 8월 28일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by