Question

I have the variables a1 a2 a3 a4.... an etc I want to find out F1 F2 F3 ...Fk.... Fn where

      F1 =     a1+a2+a3+a4+.....an
      F2 = - (sum of products of a's taken two at a time )
      F3 =   (sum of products of a's taken three at a time)
      Fk =  (-1)^(k+1)* (sum of products of a's taken k at a time)
      and so on till Fn

Can anyone help me how to get Fk through pseudo code or matlab code. Is there any built-in function in matlab which can help me in doing this ?

Was it helpful?

Solution

Use the in-built function poly(r) in matlab which takes roots of polynomial r as an array:

function Fk = get_comb(r,k)   
   p=poly(r)
   Fk=(-1)^(k)*p(k+1);
end

OTHER TIPS

This might be what you need -

A = round(10.*rand(13,1)); %// Input data, which you need to replace with your data

out = NaN(numel(A),1); %// Stores the output
for d = 1:numel(A)  
    extended_len = ceil(numel(A)/d)*d;

    A_ext = ones(extended_len,1);
    A_ext(1:numel(A)) = A;
    A_ext_res = reshape(A_ext,d,numel(A_ext)/d);

    A_prod = cumprod(A_ext_res,1);
    out(d) = sum(A_prod(end,:));
end
out(2:2:end)=-1.*out(2:2:end);

NOTE: You may create a function out of the above script, which would have input as 'A' and output as 'out'.

Printing the input and output -

A =

     5
     8
     1
     4
     9
     8
    10
     7
     0
     8
     9
     7
     8

>> out

out =

          84
        -257
         840
       -5208
        1944
      -11528
      115200
     -806400
        4032
        -504
          56
          -8
           0

The above results could be verified from the fact that the first element of output is the sum of all elements and the last one is the product of them, with all alternate elements being multiplied by -1.

You might take a look at the kron() function. It's not a complete solution to your problem, but it may help you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top