We help IT Professionals succeed at work.

Loop with matrices. Please help me!

Hamood Hamood
on
144 Views
Last Modified: 2017-03-24
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!
Comment
Watch Question

aikimarkSocial distance; Wear a mask; Don't touch your face; Wash your hands for 20 seconds
CERTIFIED EXPERT
Top Expert 2014

Commented:
What is the "difference in the weights"?

Author

Commented:
The "difference in the weights" is each value of the weight minus the value of the previous weight.
Like this:
Weights.png
aikimarkSocial distance; Wear a mask; Don't touch your face; Wash your hands for 20 seconds
CERTIFIED EXPERT
Top Expert 2014

Commented:
repeat until all the weight differences in two adjacent sets are < .001 or until a single comparison is < .001?

Author

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
aikimarkSocial distance; Wear a mask; Don't touch your face; Wash your hands for 20 seconds
CERTIFIED EXPERT
Top Expert 2014

Commented:
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.

Author

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.
Social distance; Wear a mask; Don't touch your face; Wash your hands for 20 seconds
CERTIFIED EXPERT
Top Expert 2014
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION

Author

Commented:
Im getting this error:
Untitled.png

here:
error.png
aikimarkSocial distance; Wear a mask; Don't touch your face; Wash your hands for 20 seconds
CERTIFIED EXPERT
Top Expert 2014

Commented:
Are you using my workbook?

Author

Commented:
Im using a command button in a userform
aikimarkSocial distance; Wear a mask; Don't touch your face; Wash your hands for 20 seconds
CERTIFIED EXPERT
Top Expert 2014

Commented:
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.

Author

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!
aikimarkSocial distance; Wear a mask; Don't touch your face; Wash your hands for 20 seconds
CERTIFIED EXPERT
Top Expert 2014

Commented:
That is a new feature, so that should be a new question.

Author

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.

Author

Commented:
alright I will post a new question, but I need you to answer it please <3. thanks a lot

Author

Commented:
Big thanks to aikimark for his great help!

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions