+ Reply to Thread
Results 1 to 8 of 8

How to multiply each cell with another in an array

Hybrid View

  1. #1
    Registered User
    Join Date
    04-07-2013
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    4

    How to multiply each cell with another in an array

    Hi guys, wondering if you would be able to help me with what is probably a very easy problem to solve, but im a bit of a VBA novice and im not sure how to go about doing this in excel. I have a 3x3 grid of numbers, lets assume A1:C3. I would like to take each individual cell in that array and add all the inverses of each cell together...

    a1-1+b1-1+c1-1
    a1-1+b1-1+c2-1
    a1-1+b1-1+c3-1
    a1-1+b2-1+c1-1
    a1-1+b2-1+c2-1

    etc etc.

    And I would like the results to be put on an adjacent column, or perhaps a new sheet.

    Could anyone give me an idea how I might go about doing this please?

    Many Thanks

    Miguel

  2. #2
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,788

    Re: How to multiply each cell with another in an array

    Not sure which data present in which cell can you please attach a sample workbook with expected outcome?


    If your problem is solved, then please mark the thread as SOLVED>>Above your first post>>Thread Tools>>
    Mark your thread as Solved


    If the suggestion helps you, then Click *below to Add Reputation

  3. #3
    Registered User
    Join Date
    04-07-2013
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: How to multiply each cell with another in an array

    Ok so here is some sample data. All just numbers in a 3x3 grid.

    Id like an output column like the one ive added in. I did all of those by hand, im sure there is a relatively easy way to do this with VBA code so I dont have to write out all those formulas by hand, and then if I want to change the size of my grid later it will be a lot easier than modifying or adding another load of formulas.

    Hope that makes sense

    This sample just multiplies the values together, Im not too fused about the function, im more interested in how you go about coding it, I can always change it to inverses or whatever later...
    Attached Files Attached Files

  4. #4
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,788

    Re: How to multiply each cell with another in an array

    Thanks for the file but I am unable to understand the logic It will be helpful if you add those values nearby the output cell for better understanding.

  5. #5
    Registered User
    Join Date
    04-07-2013
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: How to multiply each cell with another in an array

    lol, now im a little confused, im not too sure how else to explain it, erm ok mathematically:

    column 1 = x
    column 2 = y
    column 3 = z

    And assuming the numbers in column 1 are all the values of x, column 2 has values for y and column 3 has values for z.

    I want to do xn * yn * zn so every permutation is calculated. In my example each column has 3 values (3x3) so that means there are only 3 values of x, y and z to calculate which means I should end up with a list of 33 solutions.

    Did that make more sense?

  6. #6
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,410

    Re: How to multiply each cell with another in an array

    It looks to me like you want to create a column that computes the product of each combination/permutation. So, in many ways, this looks to me like a combination/permutation type problem (which I never do, so I'm not very good at programming them). My approach might look something like this (comma delimited):
    product,Aindex,Bindex,Cindex,Avalue,Bvalue,Cvalue
    =product(I2:K2),1,1,1,index(A1:A3,F2),copy of I2,copy of I2
    copy of E2,1,1,2,copy of I2,copy of I2,copy of I2
    ...
    The part that is left to figure out is what formulas to put in columns F, G, and H that will generate the permutations. In your example, you appeared to generate the permutations in lexicographic order, so if we can come up with a set of cell formulas that will recreate that logic, we should be able to do this. Maybe something like:
    aindex,bindex,cindex
    1,1,1
    if(and(G2=3,H2=3),F2+1,F2),if(H2=3,G2+1,G2),=if(H2=3,1,H2+1)
    copy of F2,copy of G2,copy of H2
    ...
    though this might be tedious to extend to larger matrices.

    In the spirit of brainstorming, would there possibly be a matrix function/multiplication that could represent this problem? If one could see a matrix representation of the problem, then we could possibly use one of the built in matrix functions (like MMULT()) to get at this.
    Last edited by MrShorty; 04-09-2013 at 09:36 AM.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  7. #7
    Registered User
    Join Date
    04-07-2013
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: How to multiply each cell with another in an array

    Cheers, thats an approach I never really thought about using. I did think about using matrices but the problem I had was that I couldnt think/remember how to create a function which represented what I was doing. Its been a long time since I did anything with matrices. You're right about me using a lexicographic order when I did it, but any order would be ok, its just what came to mind first.

    Ive come up with one way of doing it which involves loops, and once again because my knowledge of programming is rather basic ive done it from a first principle approach which is probably very inelegant, and ive used a goto to create the loop which I think is probably a no-no. Anyway this is what I came up with:

    Sub test()
    
    'Define NL as the last row with information in the A column in the Main worksheet.
    Dim NL As Long
        NL = Worksheets("Main").Range("A1").End(xlDown).Row
    
    'Define FL as the last row with information in the F column in the Main worksheet
    Dim FL As Long
        FL = Worksheets("Main").Range("F65536").End(xlUp).Row
        
    'Define x y and z as column names
        
    Dim x As Long
    Dim y As Long
    Dim z As Long
        
    x = 1
    y = 1
    z = 1
        
    'Beginning of Loop
        
    iLoop:
     
        If y > NL Then
            x = x + 1
            y = 1
            z = 1
        End If
        
        If x > NL Then End
    
        If z <= NL Then
        
            'Calculate the values of x*y*z
            Worksheets("Main").Cells(FL, 6).Value = Worksheets("Main").Cells(x, 1) * Worksheets("Main").Cells(y, 2) * Worksheets("Main").Cells(z, 3)
            'Write the co-ordinates in adjactent cell for reference
            Worksheets("Main").Cells(FL, 5).Value = x & ", " & y & ", " & z
            z = z + 1
            FL = FL + 1
        
    
            Else
    
                z = 1
                y = y + 1
    
        End If
    
    GoTo iLoop
    
    
    End Sub
    Last edited by el_miguel42; 04-09-2013 at 11:06 AM.

  8. #8
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,410

    Re: How to multiply each cell with another in an array

    and ive used a goto to create the loop which I think is probably a no-no.
    I don't know that it is a no-no, but I think a lot of programmers would use it as a method of last resort. I think I would have used For...Next loops (http://msdn.microsoft.com/en-us/libr...=VS.71%29.aspx) for creating the permutations (pseudocode):
    m=0 'm will count our position in the output array
    for x=1 to n 'n is the number of rows in each vector
    for y=1 to n
    for z=1 to n
    m=m+1
    resultarray(m)=A(x)*B(y)*C(z)
    next z
    next y
    next x

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1