Loop with matrices. Please help me!

Hello everyone.
Please I really need help.
I have a matrix that I want to square (multiplying it by itself).
and then I'm gonna add the value of each row of this matrix, and then add the value of this column and divide each value by the total.
Look at this picture and you will understand what I mean.
Weights.pngI need a code to repeat this process until the difference in the weight is < 0.01 (probably using loops).

Im using this code to square the matrices:
  Dim xArray As Variant, zArray As Variant
    Dim Fn As Object
    Set Fn = Application.WorksheetFunction
    xArray = Range("g11:j14").Value
    zArray = Fn.MMult(xArray, xArray)
     
    ActiveCell.Resize(4, 4).Value = zArray
  

Open in new window

Please help me!
and thanks in advance!
Hamood HamoodAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

aikimarkCommented:
What is the "difference in the weights"?
0
Hamood HamoodAuthor Commented:
The "difference in the weights" is each value of the weight minus the value of the previous weight.
Like this:
Weights.png
0
aikimarkCommented:
repeat until all the weight differences in two adjacent sets are < .001 or until a single comparison is < .001?
1
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

Hamood HamoodAuthor Commented:
Repeat until the weight differences in two adjacent sets are < 0.001
Which means
A-a < 0.001 and B-b <0.001 and C-c <0.001 and D-d<0.001
0
aikimarkCommented:
You could set up a formula that checks that
=And((L15-L9)<.001,(L16-L10)<.001,(L17-L11)<.001,(L18-L12)<.001)

Open in new window


Then, select two adjacent blocks and the checker cell and do a fill down, dragging the selection by the lower right corner until the checker value equals True.
0
Hamood HamoodAuthor Commented:
What I need is a loop that keeps squaring the matrix until the difference between the weight of the squared matrix and the weight of the previous matrix is < 0.001. That's what I mean bro.
I already know the code to square the matrix. but I dont know how to create a loop that keeps squaring the matrices.
Thanks in advance.
0
aikimarkCommented:
Try this
Sub Q_29010395()
    Dim wks As Worksheet
    Dim rngPrior As Range
    Dim rngCurrent As Range
    Dim rng As Range
    Dim boolStop As Boolean
    
    Dim vArray As Variant   ', zArray As Variant
    Dim Fn As Object
    
    Set Fn = Application.WorksheetFunction
    Set wks = ActiveSheet
    Set rngPrior = wks.Range("G6").CurrentRegion
    Set rngCurrent = rngPrior.End(xlDown).Offset(3)
    
    Do
        vArray = rngPrior.Value
        'zArray = Fn.MMult(xArray, xArray)
        
        rngCurrent.Resize(4, 4).Value = Fn.MMult(vArray, vArray)
        rngCurrent.CurrentRegion.Columns(4).Offset(0, 2).FormulaR1C1 = "=sum(rc[-5]:rc[-2])"
        rngCurrent.CurrentRegion.Columns(4).Offset(0, 3).FormulaR1C1 = "=rc[-1]/sum(r" & rngCurrent.Row & "c[-1]:r" & rngCurrent.Row + 3 & "c[-1])"
        boolStop = True
        For Each rng In rngCurrent.CurrentRegion.Columns(4).Offset(0, 3).Cells
            If (rng - rng.Offset(-6, 0)) < 0.001 Then
            Else
                boolStop = False
                Exit For
            End If
        Next
        If boolStop Then
            Exit Do
        Else
            'set up next iteration
            Set rngPrior = rngCurrent
            Set rngCurrent = rngCurrent.End(xlDown).Offset(3)
            Set rngPrior = rngPrior.CurrentRegion
        End If
        'Stop
    Loop Until boolStop
End Sub

Open in new window

Q_29010395.xlsm
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Hamood HamoodAuthor Commented:
Im getting this error:
Untitled.png

here:
error.png
0
aikimarkCommented:
Are you using my workbook?
1
Hamood HamoodAuthor Commented:
Im using a command button in a userform
0
aikimarkCommented:
Did you look at my workbook?

The code assumes a certain configuration of cells to start.  If your worksheet does not have that configuration, I would expect the code to fail.
1
Hamood HamoodAuthor Commented:
Oh, my bad. It's working perfectly!! thank you so much indeed!!
1 last request, please. I want to record the results of the last weights in a fixed range of cells.
I mean no matter how I change the values of the matrix. after the loop ends, I want the results of the last weights recorded in some fixed range of cells. Let's say cells (O6:O9). can you please help me? and thank you a lot!!! you are truly a life saver!
0
aikimarkCommented:
That is a new feature, so that should be a new question.
1
Hamood HamoodAuthor Commented:
and can you plz show me what changes do I need to make to the code in case I wanted to change the matrix into 5x5?
Thanks a lot in advance.
0
Hamood HamoodAuthor Commented:
alright I will post a new question, but I need you to answer it please <3. thanks a lot
0
Hamood HamoodAuthor Commented:
Big thanks to aikimark for his great help!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VBA

From novice to tech pro — start learning today.