0
comdata(0, 0) = "5"
comdata(0, 1) = "3"
comdata(0, 2) = "10"
comdata(0, 3) = Nothing
comdata(1, 0) = "1"
comdata(1, 1) = "7"
comdata(1, 2) = "14"
comdata(1, 3) = Nothing
comdata(2, 0) = "5"
comdata(2, 1) = "8"
comdata(2, 2) = "14"
comdata(2, 3) = Nothing

I have an array that looks like above. I would like to make something like below. The idea is when there are same value on the array comdata(,), for example there are two value 5, on the comdata(0, 0) and comdata(2, 0). I would like to set value on the comdata(2, 0).

choose(0,0) = True  
choose(0,1) = True  
choose(0,2) = True  
choose(0,3) = False
choose(1,0) = True  
choose(1,1) = True
choose(1,2) = True  
choose(1,3) = False
choose(2,0) = False 
choose(2,1) = True  
choose(2,2) = False 
choose(2,3) = False 

I have try it with the code below, unfortunately when the value of i = 2, it cannot check the comdata(0, 0), comdata(0, 1), comdata(0, 2), comdata(0, 3). So the value of choose(0, 0), choose(0, 1), choose(0, 2) will be set to true.

For i = 0 To 2
    For j = 0 To 2
        If comdata(i, j) <> Nothing Then
            If i = 0 Then
                choose(i, j) = True
            Else
                For k = 0 To 2
                    If comdata(i, j) = comdata(i - 1, k) Then
                        choose(i, j) = False
                    Else
                        choose(i, j) = True
                    End If
                Next
            End If
        End If
    Next
Next
1
  • Is it important that the second index of matching value is the same in both cases when choose is set to False on a non-empty instance? For example, if comdata(0,0) and comdata(1,2) are both "5", should choose(1,2) be set to False? Commented Dec 17, 2013 at 11:24

2 Answers 2

1

It's a bit confusing what you are trying to do. When you loop for "k" you only look for values inside "i" you need to check all values. You're also only looping to 2 when your array can definetly go to 3.

    For x As Integer = 0 To 2
        For y As Integer = 0 To 3
            If x = 0 And y = 0 Then
                choose(x, y) = True
            ElseIf comdata(x, y) IsNot Nothing Then
                choose(x, y) = True

                For u As Integer = 0 To 2
                    For v As Integer = 0 To 3
                        If x = u And y = v Then
                            Exit For
                        End If

                        If comdata(x, y) = comdata(u, v) Then
                            choose(x, y) = False
                        End If
                    Next

                    If u = x Then
                        Exit For
                    End If
                Next
            End If
        Next
    Next
Sign up to request clarification or add additional context in comments.

Comments

0

Some of your code doesn't make sense to me: I don't see what the purpose of If i = 0 Then ... is. This has the effect of setting your first three elements to True even though two of them have no duplicate. Ignoring that for now and carrying on...

You're trying to do this with three loops, but you need to use four: two outer loops (i and j define the current element being compared), and two inner loops (k and a new loop that define the element being compared to).

So, where you're currently using comdata(i - 1, k), you instead need a further loop (For l = 0 To 2) and replace i - 1 with l.

Note that you will need a further guard clause in this case, because at some point in your loop you will compare your element to itself. That is, if i = l and j = k then you should not make the comparison - otherwise each element will treat itself as a duplicate.

2 Comments

Actually I use If i = 0 Then . . . to always give the first three element True value. <br> I'm sorry, I am a little bit confuse on how to implement your suggestion. Would you like to write the code on this comment section. Thank you
I am now confused also: why are the first three elements always true? Also, I notice now that your loop on j is only 0 to 2 - is that intentional?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.