Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
matrix: multiply each element by each combination of all other element

Subject: matrix: multiply each element by each combination of all other element

From: Cliff

Date: 6 Jun, 2011 14:32:02

Message: 1 of 3

This should be a simple problem but I haven't been able to figure out an efficient way to do it. If I have a matrix, or vector, and I want to multiply each element by every other element, and every combination of multiples of every other element, what is the best way to do it?

for ex: [1 2 3 4 5] would go to

[1, 2, 3, 4, 5, 2*3, 2*4, 2*5, 2*3*4, 2*4*5, 3*4*5, 4*5, 3*5, 3*4, 2*3*4*5]

Also, is there a simple way to test for and eliminate identical elements?
For example, [ 1 2 3 3 3 4 5] could go to [1 2 3 4 5] or [1 2 3 0 0 4 5].

I've tried messing around with multi-dimensional arrays, and thought of crazy for loops, but those seem unneccesarily complicated for what should be a simple problem.

Answering this^ would be helpful - the real problem i'm trying to do though, is to find all the divisors of a given number. Using factor(x) obviously gives all the prime factors.

Thanks

Subject: matrix: multiply each element by each combination of all other element

From: Roger Stafford

Date: 6 Jun, 2011 16:34:08

Message: 2 of 3

"Cliff" wrote in message <isiod2$dmo$1@newscl01ah.mathworks.com>...
> This should be a simple problem but I haven't been able to figure out an efficient way to do it. If I have a matrix, or vector, and I want to multiply each element by every other element, and every combination of multiples of every other element, what is the best way to do it?
>
> for ex: [1 2 3 4 5] would go to
>
> [1, 2, 3, 4, 5, 2*3, 2*4, 2*5, 2*3*4, 2*4*5, 3*4*5, 4*5, 3*5, 3*4, 2*3*4*5]
> .........
- - - - - - - - - - -
  Let v be a row vector.

 n = size(v,2);
 p = ones(2^n,1);
 m = 1;
 for k = 1:n
  t = prod(nchoosek(v,k),2);
  p(m+1:m+length(t)) = t;
  m = m + length(t);
 end

This should get all possible products of v elements in vector p.

Roger Stafford

Subject: matrix: multiply each element by each combination of all other element

From: Roger Stafford

Date: 6 Jun, 2011 17:15:21

Message: 3 of 3

"Roger Stafford" wrote in message <isivhv$7hq$1@newscl01ah.mathworks.com>...
> "Cliff" wrote in message <isiod2$dmo$1@newscl01ah.mathworks.com>...
> > This should be a simple problem but I haven't been able to figure out an efficient way to do it. If I have a matrix, or vector, and I want to multiply each element by every other element, and every combination of multiples of every other element, what is the best way to do it?
> >
> > for ex: [1 2 3 4 5] would go to
> >
> > [1, 2, 3, 4, 5, 2*3, 2*4, 2*5, 2*3*4, 2*4*5, 3*4*5, 4*5, 3*5, 3*4, 2*3*4*5]
> > .........
> - - - - - - - - - - -
> Let v be a row vector.
>
> n = size(v,2);
> p = ones(2^n,1);
> m = 1;
> for k = 1:n
> t = prod(nchoosek(v,k),2);
> p(m+1:m+length(t)) = t;
> m = m + length(t);
> end
>
> This should get all possible products of v elements in vector p.
>
> Roger Stafford
- - - - - - - -
  Or here is a vectorized method. Again let v be a row vector.

 n = size(v,2);
 t = dec2bin(0:2^n-1,n)-'0';
 p = prod(bsxfun(@times,v,t)-t+1,2);

Roger Stafford

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us