I have to compare up to 5 numbers in my script. That also works quite well. However, my first and last number are not compared. What's the problem in my code?
local loaded = game.Workspace.TrommelValue.Value
local randomkugel = {}
for i = 0,loaded-1 do
randomkugel[i] = math.random(1,6)
for index = 0, i+1 do
if i ~= index then
if randomkugel[i]==randomkugel[index] then
randomkugel[i]=math.random(1,6)
index = 0
end
end
end
end
Thank you for helping me!
I don't know what game.Workspace.trommelValue.value is, but let's assume its a positive number.
First iteration
-- assign a random integer from [1-6] to `randomkugel[0]`
randomkugel[0] = math.random(1,6)
Now you run the inner loop the first time. The first cycle is skipped as i == index.
The second cylcle is skipped because randomkugel[i] is the random number and randomkugel[index] is nil
Second iteration of the outer loop. i is 1, random[1] is assigned a random value from [1-6].
Inner loop:
first run i is 1, index is 0 so the first if statement is entered.
randomkugel[1] may randomly equal randomkugel[0]. In that case you would assign a new random value to randomkugel[1] and set index to 0 which does not have any effect.
In case both values are not equal nothing happens.
second cycle of the inner loop, i is still 1, index is 2.
As there is no randomkugel[2] this cycle does nothing.
You will always skip the last cycle of the inner loop as you're comparing vs nil every time.
So your inner loop is effectively
for index = 0, i do
if i ~= index then
if randomkugel[i]==randomkugel[index] then
randomkugel[i]=math.random(1,6)
end
end
end
I guess your greatest misconception here is that you can somehow reset the loop counting variable index inside the loop body.
Any change to index is only valid after that change within the current iteration.
Related
I will explain my problem. I have a 1x1701 sampled array "resampled_WF", once I found the max value of this array ("peak_WF"), I set a threshold 0.7*peak_WF, and I would like to select the farthest value in the array that gets nearer to this threshold.
Example:
power_vs_time_threshold
As you can see, I was able to select ony the first value that resembles... but I would like to get the last one (around t=2 sec).
I tried to flip the array with "flip" function:
WF_threshold_input = 0.7*peak_WF;
flip_resampled_WF = flip(resampled_WF);
diff_peak_threshold = peak_WF - WF_threshold_input; %power loss at 70% power reduction
diff_peak_WF = peak_WF - flip_resampled_WF;
min_diff_threshold = min(abs(diff_peak_WF-diff_peak_threshold));
Doing that, MATLAB computes minimum difference on the whole array, I would like to stop at the first value, not considering further values.
I tried to select values with values <= WF_threshold_input, but again it selects over the whole dataset.
How can I select the value properly?
Thanks!!
Using operations on the matrix will call the operation on every element in the matrix. What you want to do is use a loop so that you can control exactly what elements are examined and then break out of the loop.
index = length(resampled_WF);
your_threshold = ...
while resampled_WF(index) < your_threshold
index = index - 1;
end
The while loop will continue iteration until it reaches a value that is outside of your defined threshold.
After execution, the value of index will be the index of the furthest value in the array which is outside of your threshold. You can access the furthest value outside the threshold by looking at resampled_wf(index) after execution of the code.
We don't have to worry about the values of index leaving the bounds of the array, ie <1, since the condition resampled_wf < your_threshold is guaranteed to be met by the maximal value that you originally generated the threshold with.
package main
import (
"fmt"
)
func main() {
m := make(map[int]int, 4)
m[1] = 0
m[2] = 0
for k, _ := range m {
i := 10 + k
m[i] = 0
}
fmt.Println(m)
fmt.Println("len:", len(m))
}
This code returns: 8 or 10 or 6 as length of map after loop.
Video is here, playgroud here.
I see that new added elements go into range, but can't explain why this loop stops randomly?
Spec: For statements:
The iteration order over maps is not specified and is not guaranteed to be the same from one iteration to the next. If a map entry that has not yet been reached is removed during iteration, the corresponding iteration value will not be produced. If a map entry is created during iteration, that entry may be produced during the iteration or may be skipped. The choice may vary for each entry created and from one iteration to the next. If the map is nil, the number of iterations is 0.
The spec states that if you add entries to the map you are ranging over, the elements you add may or may not be visited by the loop, and moreover, which is visited is not even deterministic (may change when doing it again).
You are modifying map you are iterating over. This is the cause.
Is it possible to index an array in a loop with in a loop (Matlab)?
So I'm indexing a 3d matrix with 3 parameter:
For instance:
A(1,1,:)
A(1,2,:)
...
A(1,9,:)
then I want the code to jump to the next element of the first prameter:
A(2,1,:)
A(2,2,:)
...
A(2,9,:)
And so on until I finish with all the elements of the first parameter.
Now I have a :
for j=1:27
for i=1:9
X(j,i,:)= f(j,i,:)
end
end
But I'm only getting a row of results when it should be a matrix of 27x9x:
Do anyone know how to make the second loop (i) runs 9 times for the first value of j and then move to the second value of j and run 9 times i and so on?
Thanks!
use squeeze(); something like this:
for j=1:27
for i=1:9
tmp= squeeze(f(j,i,:));
X(j,i,:) = tmp;
end
end
I am trying to loop through an array that contains split strings, done via this line:
repHolder() = Split(rep, ",")
That's all fine and good, however, when I try to loop through this repHolder() array via a for loop, I am met each time with a subscript out of range error.
This makes no sense to me. When I step through the array it fails on the first element every time; this line:
If repHolder(j) = counter Then
I tried setting j to 0 and 1, both of which failed on the first sequence of the loop. This suggests to me because the array doesn't have a defined size; that I cannot loop through it this way, but that still makes little sense to me as it is still filled with elements.
Here is the entire code block of what I am trying to do:
Dim repHolder() As String
Dim strHolder() As String
Dim counter As Variant
Dim j As Integer
For Each rep In repNames()
repHolder() = Split(rep, ",")
Next rep
For Each rangeCell In repComboRange
k = 1
Do
If rangeCell.Value = repCombos(k) Then 'At this point, if rangecell = repcombos(k)
Range(rangeCell, rangeCell.End(xlToRight)).Copy
strHolder() = Split(rangeCell.Value, "/")
For Each counter In strHolder()
Stop
For j = 1 To 17
If repHolder(j) = counter Then
You are looping through repNames() and setting this new array via split (over and over again for each repName element...)
For Each rep In repNames()
repHolder() = Split(rep, ",")
Next rep
Every iteration of this loop resets repHolder() to a split of the rep element dropping whatever values were just set in that array in the previous iteration. So once it's done only the last element of RepNames() has been split into the repHolder() array.
For instance, if RepNames() looks like:
Element 0: "james,linda,mahesh,bob"
Element 1: "rajesh,sam,barb,carrie"
Element 2: ""
Then after all this iterating your repHolder array is going to be empty because there is nothing in the final element.
Stick a breakpoint (F9) on you For Each rangeCell In repComboRange line and look at your Locals pane in VBE. Check out the values that are stored in your repHolder() array at that point in time. I suspect there will be nothing in there.
The other oddball here is that you are looping 1 through 17. repHolder() will be a 0-based array so that should be 0 through 16. But... even that is nonsense since this really only makes sense as a For Each loop (or to use the uBound(repHolder) to determine how many times to loop:
For Each counter In strHolder()
Stop
For each repHolderElem in repHolder
If repHolderElem = counter Then
....
Next repHolderElem
Hi I'm working on a macro in VBA for excel. I have a nested for loop in my code as seen below. The second loop does not run; even a MsgBox() command within the second loop doesn't result in any action and the program just seems to skip over the nested loop with no reported errors.
In plain English this nested loop should:
1) Take the string from the i entry in the array categories_string (first for loop).
2) iterate through the 300+ rows in column "AE" in the excel file (second for loop, "length" is the length of the column of data in "AE")
3) look for string matches and add 1 to the corresponding entry in the categories_value array which is populated with variables set to 0 (the if statement).
For i = LBound(categories_string) To UBound(categories_string)
For p = 1 To p = length
If Worksheets("sheet1").Cells(p + 2, "AE").Value = categories_string(i) Then
categories_value(i) = categories_value(i) + 1
End If
Next
Next
Change
For p = 1 To p = length
to
For p = 1 To length
Should fix you right up.
You may want to consider Scott Cranner's comment as well, especially if your categories_string is large and length is large. Or, just do it for the practice