Click here to Skip to main content
15,895,142 members
Home / Discussions / Algorithms
   

Algorithms

 
GeneralRe: ceiling function bitwise operation [Solved] Pin
harold aptroot7-Nov-13 6:12
harold aptroot7-Nov-13 6:12 
GeneralRe: ceiling function bitwise operation [Solved] Pin
econy20-Nov-13 5:48
econy20-Nov-13 5:48 
GeneralRe: ceiling function bitwise operation [Solved] Pin
harold aptroot20-Nov-13 6:07
harold aptroot20-Nov-13 6:07 
GeneralRe: ceiling function bitwise operation [Solved] Pin
econy20-Nov-13 9:23
econy20-Nov-13 9:23 
QuestionSpiral Function Pin
push_coder30-Oct-13 18:41
push_coder30-Oct-13 18:41 
AnswerRe: Spiral Function Pin
Bernhard Hiller31-Oct-13 1:26
Bernhard Hiller31-Oct-13 1:26 
AnswerRe: Spiral Function Pin
Alan Balkany31-Oct-13 11:22
Alan Balkany31-Oct-13 11:22 
QuestionRandom Triangular Matrix Pin
Kyudos28-Oct-13 11:50
Kyudos28-Oct-13 11:50 
I need a test input file for a function I'm developing - no problem I thought, I'll quickly whip one up in Excel and get on with it. I've now been trying to create the test file for three days!

It seems so simple, but probability appears to be working against me!

I need a 1000x1000 symmetric matrix with 49 'random' ones in each column, filled in reciprocally to make the thing symmetrical. Obviously as you come to the bottom corner of the matrix the available 'random' slots start to run out. Nevermind I thought, I'll wait until my code gets to the hard bit, stop it and then fill in the missing bits by hand.

Unfortunately, I always seem to be left with an insoluble problem - I can't fill in the column, because (somehow) the reciprocal spaces I 'need' have already been used. I'm sure I've made some elementary mistake, but I just can't spot it.

Can anyone fix my code? (Or come up with an algorithm that works!)

Ugly hacky VBA below:

VB
Option Explicit

Public Sub CreateRandomPeers()

    Dim TotalEntries As Integer
    Dim colEnd As Integer
    Dim rowEnd As Integer
    Dim colStart As Integer
    Dim rowStart As Integer
    Dim curCol As Integer
    Dim curRow As Integer
    Dim curEntries As Integer
    Dim rndEntries As Integer
    Dim rndRow As Integer
    Dim count As Integer
    Dim colcnt As Integer
    Dim i As Integer, j As Integer
    Dim row As Integer
    Dim col As Integer
    Dim calcmode As XlCalculation
    
    TotalEntries = 49
    colEnd = 1001
    rowEnd = 1001
    colStart = 2
    rowStart = 2
    
    calcmode = Application.Calculation
    Application.Calculation = xlCalculationAutomatic 'xlCalculationManual
    
    curRow = rowStart
    
    ' For each column, randomly add the appropriate number of entries
    For curCol = colStart To colEnd Step 1
        ' First, check if any entries have been added reciprocally from other columns
        curPeers = 0
        For i = rowStart To rowEnd Step 1
            If (Application.Cells(i, curCol) = 1) Then
                curEntries = curEntries + 1
            End If
        Next i
        
        ' Randomly assign remaining entries to the current column
        rndEntries = TotalEntries - curEntries
        Randomize
        count = 0
        While (count < rndEntries)
            DoEvents
            rndRow = CInt(Int((rowEnd - rowStart + 1) * Rnd())) + rowStart
            ' Check row and column aren't equal
            If (rndRow <> curCol) Then
                ' Check this hasn't already been set
                If (Application.Cells(rndRow, curCol) = 0) Then
                    ' Check to see if the reciprocal column has reached its limit
                    colcnt = 0
                    For j = rowStart To rowEnd Step 1
                        colcnt = colcnt + Application.Cells(j, rndRow)
                    Next j
                    If (colcnt < TotalEntries) Then
                        ' Set this entry
                        Application.Cells(rndRow, curCol) = 1
                        ' Reciprocate
                        Application.Cells(curCol, rndRow) = 1
                        ' Count
                        count = count + 1
                    End If
                End If
            End If
        Wend
    Next curCol
    
    Application.Calculation = calcmode

End Sub

Public Sub Reset()
    Application.Range("B2:ALM1001") = 0
    Application.Calculation = xlCalculationAutomatic
End Sub


modified 29-Oct-13 20:00pm.

AnswerRe: Random Triangular Matrix Pin
BillWoodruff29-Oct-13 5:29
professionalBillWoodruff29-Oct-13 5:29 
GeneralRe: Random Triangular Matrix Pin
Kyudos29-Oct-13 14:01
Kyudos29-Oct-13 14:01 
QuestionPicking persons in a group algorithm Pin
Eduard Keilholz27-Oct-13 21:50
Eduard Keilholz27-Oct-13 21:50 
AnswerRe: Picking persons in a group algorithm Pin
Chris Losinger28-Oct-13 6:01
professionalChris Losinger28-Oct-13 6:01 
SuggestionRe: Picking persons in a group algorithm Pin
Richard Deeming28-Oct-13 12:57
mveRichard Deeming28-Oct-13 12:57 
AnswerRe: Picking persons in a group algorithm Pin
Richard Deeming29-Oct-13 2:45
mveRichard Deeming29-Oct-13 2:45 
GeneralRe: Picking persons in a group algorithm Pin
BillWoodruff29-Oct-13 5:22
professionalBillWoodruff29-Oct-13 5:22 
Questionusing genetic algorithms to improve voice commands recognition Pin
Member 997361615-Oct-13 0:24
Member 997361615-Oct-13 0:24 
QuestionHow to Remove duplicate files rapidly? Pin
happy liu7-Oct-13 22:35
professionalhappy liu7-Oct-13 22:35 
AnswerRe: How to Remove duplicate files rapidly? Pin
jschell8-Oct-13 10:28
jschell8-Oct-13 10:28 
GeneralRe: How to Remove duplicate files rapidly? Pin
happy liu8-Oct-13 14:51
professionalhappy liu8-Oct-13 14:51 
AnswerRe: How to Remove duplicate files rapidly? Pin
Manfred Rudolf Bihy9-Oct-13 0:14
professionalManfred Rudolf Bihy9-Oct-13 0:14 
GeneralRe: How to Remove duplicate files rapidly? Pin
happy liu9-Oct-13 16:44
professionalhappy liu9-Oct-13 16:44 
GeneralRe: How to Remove duplicate files rapidly? Pin
Manfred Rudolf Bihy9-Oct-13 16:51
professionalManfred Rudolf Bihy9-Oct-13 16:51 
GeneralRe: How to Remove duplicate files rapidly? Pin
happy liu9-Oct-13 16:52
professionalhappy liu9-Oct-13 16:52 
GeneralRe: How to Remove duplicate files rapidly? Pin
saephoed8-Jan-14 12:50
saephoed8-Jan-14 12:50 
AnswerRe: How to Remove duplicate files rapidly? Pin
Kornfeld Eliyahu Peter9-Oct-13 0:50
professionalKornfeld Eliyahu Peter9-Oct-13 0:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.