I am trying to import numbers from files and change them if they are at a certain value. I am using torch to get values form gesture and change them from 101 to 10 or from 100 to 9 if the input is the corresponding number (10 or 9). Unfortunately, I have figured out that in Lua, the input is of type userdata which cannot be converted to integers and not compared to integers or torch tensors.
So my question is: How can I check for equality of numbers if the input type is userdata?
Is there potentially a way to convert the input to a number such that comparison is possible?
gesture = matio.load(val, 'gesture')
print(type(gesture)) --prints `userdata`
print(gesture) --prints 10 (for example)
if gesture == th.FloatTensor({101}) then
gesture = th.FloatTensor({10})
print("101 Detected! New value is: ")
print(gesture)
os.exit(0)
elseif gesture == th.FloatTensor({100}) then
gesture = th.FloatTensor({9})
print("100 Detected! New value is: ")
print(gesture)
os.exit(0)
end
Just found an easier way
local gesture_int = gesture:uint()
This is fairly complex question and after testing through the hard way, I found a way to compare the userdata number with an integer.
print(type(gesture)) -> prints `userdata`
print(gesture) -> 10
gesture_str = tostring(gesture)
print(type(gesture_str)) -> prints `string`
gesture_int = tonumber(gesture_str)
print(type(gesture_int)) -> prints `number`
Single line solution is as follows
gesture_int = tonumber(tostring(gesture))
This new variable stores the userdata number as an integer.
Related
So I'm learning C and got this exercise to do with functions. Not gonna ask how to do it.
So I created this function:
int menu(void) {
char user;
do {
printf("Choise: ");
user = getchar();
if (user == '1') {
/* code and no return here */
}
else if (user == '2') {
/* code */
return 2;
}
else if (user == '3') {
/* code */
return 3;
}
} while (user != '3');
Got others controls flows like isdigit(user) and !isdigit(user).
The problem here is that if the user input "fw" (for example), the program prints 2 times "Choise: " or more if there's more input of course.
I tried several others controls flows and changing the variable user to an array and force user[0] == ... && user[1] == '\n' but to no avail.
What I'm trying to do is, if the user don't enter one of the 3 options the program just stop reading after the 1st input and waits for another input.
Already checked several questions at StackOverflow but it doesn't answer to my question :/
I'm all ears to advises! Thank in advance!
The underlying cause here is that getchar() in C gets one single character; it's not like, for example, input() in Python, which gets an entire string of as many characters as you like. The usual technique in C to get a string consisting of more than one character is with pointers: You declare a variable, for instance, as char* a; To obtain user input, you use scanf() and store the input under that pointer address: scanf(&a); Technically, a is the pointer pointing at the memory address of the first character in your string, but the compiler stores the individual characters of the string in a contiguous block of memory until a null character is reached to mark the end of the string.
To avoid the risk of seg faults, you might want to reserve as much memory as you need to hold the longest string you think you'll need: a = malloc((sizeof(char)*n);, with n being the number of characters you want to set memory aside for.
Apologies for not posting in a while, I've been travelling. I thought about the problem again, and I think the root of the problem is that getchar() will return the character cast into an integer, namely, the ASCII value of the character. So if the user keys in "1", you can't run an if statement that tests if (user == '1') (C doesn't natively support direct string comparison). Instead, you should test if (user == 49) - 49 is the ASCII value of the digit "1" ("2" is 50, "3" is 51). If you write your if statements and the loop condition accordingly, that should work.
I have this function below that iterates through every row of a data frame (using pandas apply) and determines what values are valid from a prediction-probability matrix (L2) by referencing another data frame (GST) to obtain the valid values for a given row. The function just returns the row back with the maximum valid probability assigned to the previously blank value for that row (Predicted Level 2) in the data frame passed to the function (test_x2)
Not a terribly complex function and it works fine on smaller datasets but when I scale to like 3-5 million records, it starts to take way too long. I tried using the multiprocessing module as well as dask/numba but nothing was able to improve the runtime (not sure if this is just due to the fact the function is not vectorizable).
My question is two fold:
1) Is there a better way to write this? (I'm guessing there is)
2) If not, what parallel computing strategies could work with this type of function? I've already tried a number of different python options but I'm just leaning more towards running the larger datasets on totally separate machines at this point. Feel free to provide any suggested code to parallelize something like this. Thanks in advance for any guidance provided.
l2 = MNB.predict_proba(test_x)
l2_classes = MNB.classes_
L2 = pd.DataFrame(l2, columns = MNB.classes_)
test_x2["Predicted Level 2"] = ""
def predict_2(row):
s = row["Predicted Level 1"]
s = GST.loc[s,:]
s.reset_index(inplace = True)
Valid_Level2s = s["GST Level 2"].tolist()
p2 = L2.ix[row.name, Valid_Level2s]
max2 = p2.idxmax(axis = 1)
output = row["Predicted Level 2"] = max2
return row
test_x2 = test_x2.apply(predict_2, axis = 1)
I have a function returning a setof records. This can be seen in this picture
.
I have a range of boards of length 2.8m thru to 4.9m (ln28 thru ln49 respectively) they have characteristics that set bits as seen in bincodes (9,2049,4097 etc.) For each given board length, I need to sum the number of boards for each bincode. EG in this case ln28 (bincode 4097) would = 3+17+14 = 34. Where you see brdsource = 128 series is where I intend to store these values, so for row brdsource 128, bincodes 4097, I want to store 34 in ln28.
You will see that I have 0's in ln28 values for all brdsource = 128. I have generated extra records as part of my setof records, and am trying to use a multidimensional array to add the values and keep track of them as seen above with array - Summary[boardlength 0-8][bincode 0-4].
Question 1 - I see that if I add 1 (for argument sake, it can be any number) to an array location, it returns a null value (no error, just nothing in table cell). However if I first set the array location to 0, then add 1, it works perfectly. How can an array defined as type integer hold a null value?
Question 2 - How do I add my respective record (call it rc) board length count to the array. IE I want to do something like this
if (rc.bincode = 4097) then Summary[0][2] := Summary[0][2] + rc.ln28;
and then later, on, when injecting this into my table (during brdsource = 128 phase) :
if (rc.bincode = 4097) then rc.ln28 := Summary[0][2];
Of course I may be going about this in a completely unorthodox way (though to me SQL is just plain unorthodox, sigh). I have made attempts to sum all previous records based on the required conditions (eg using a case(when...end) statement, but I proved what I already suspected, ie that each returned record is simply a single row of data. There is just no means of accessing data in the previous record lines as returned by the functions FOR LOOP...END LOOP.
A final note is that everything discussed here is occurring inside the function. I am not attempting to add records etc. to data returned by the function.
I am using PostgreSQL 9.2.9, compiled by Visual C++ build 1600, 64-bit. And yes I am aware this is an older version.
I am trying to get and send my MCU's IP Adress, SubnetMask and Gateway adress.
I got them but problem is merging them. I want to merge them with array and send for one step..
For example:
my values are
e2promIpAddress = 0A020705 // represents 10.2.7.5
e2promSubnetMask = FFFF0000 // represents 255.255.0.0
e2promGateway = 0A02070F // represents 10.2.7.15
When I add with sprintf()
char buffer[64];
sprintf(buffer,"%x%x%x",e2promIpAddress,e2promSubnetMask,e2promGateway);
Output is A020705FFFF00000A02070F
Unfortunatelly array must start with 0 but it goes away..
Thanks in advance
I finally find my answer and want to post here..
My values for example e2promIpAddress = 0A020705 is 4 bytes.
When I wrote this with;
sprintf(buffer,"%02x%02x%02x",e2promIpAddress,e2promSubnetMask,e2promGateway);
it did not pad "0"
when I wrote this with;
sprintf(buffer,"%08x%08x%08x",e2promIpAddress,e2promSubnetMask,e2promGateway);
all values which starts with "0" pad with "0"
Have a good day..
I am trying to "visualize" a list of numbers from a .csv file.
I could do that by writing a blender script and thats what I already did.
But I would like to know how the Add Mesh: Extra Object - Math Function could help me (or other functions/addons/anything).
I would like to have a 2D Line (x=+1, y=0, z= value from list) that I would rotate afterwards.
In other words can I convert a list of numbers into a function ?
Sorry if that is a stupid question.
Thanks for the help.
Where The Extra Object - Math Function addon uses minU maxU and stepU as user input values that are used to calculate the value of u before evaluating the function to generate the mesh, you want to let the user input an int value and use it as an index into an array -
AvailZOptions = [1.3, 2.6, 5.2, 10.4]
lineEnd = [xvalue, yvalue, AvailZOptions[OptZUserChoice]]
Another option would be to get the zValue from a function -
def CalcZValue:
if OptZUserChoice = 1:
return 1.3
elif OptZUserChoice = 2:
return 2.6 * cos(x)
lineEnd = [xvalue, yvalue, CalcZValue]