How to write to arrays using loops in PureData? - arrays

I'm writing random numbers in an array using loops in Pure Data. For some reason in loop one Pure Data starts writing from index 1 instead of 0 and finishes on index 0 after the loop, this is not what I want. In loop two I found a solution using an extra [f] and [bang], Pure Data does the expected, starting from index 0. In both cases console prints the data in the same order (numbers are random, the order is the same), just the graph, reflecting the array, is different.
Does anyone know why the first case fails and the second works? My guess is that its related to the flow of data into the left and right inlets of [tabwrite].
loop one
Code
Console
Graph (1.151 is on index 1 instead of index 0)
loop two
Code
Console
Graph (1.742 is correct on index 0)

Message order.
You have a fanning output on the [f] object. Use a trigger [t b f] (short for [trigger bang float]) to make the order explicit. If you see an atom with multiple connections on one output, that's a red flag! Always use trigger objects, because without them the order in which you made the connections will be the order in which the messages will be sent. That's not visible in the patch, so it should be avoided!
Read more about message order in the documentation of Pd
There is another issue in your patch. Consider this case: If NotasPor0it is 8 and the counter is at 6, then NotasPor0it is changed to 4, the select object [sel] will never trigger the reset function of the counter idiom and continue to count... To avoid this issue, you could use a 'larger as' [>] symbol, or solve it with modulo (%) as seen in the screenshot.

Related

Storing and replacing values in array continuously

I'm trying to read amplitude from a waveform and shine a green, yellow or red light depending on the amplitude of the signal. I'm fairly new to labVIEW and couldnt get my idea that wouldve worked with any other programming language I know to work. What I'm trying to do is take the value of the signal and for everytime it updates I'll store the value of the amplitude into an index of a large array. With each measurement being stored in the n+1 index of the array.
After a certain amount of data points I want to start over and replace values in the array (I use the formula node with the modulus for this). By keeping a finite amount of indexes to check for max value I restrict my amplitude check to a certain time period.
However my problem is that whenever I use the replace array subset to insert a new value into index n, all the other index points get erased. Rendering it pretty much useless. I was thinking its the Initialize array causing problems but I just cant seem to wrap my head around what to do here.
I tried creating just basic arrays in the front panel, but those either are control or indicator arrays and can't seem to be both written and read from, its either control (read but not write) or indicate(write but not read)?. Maybe its just not possible to do what I had in mind in an eloquent way in LabVIEW. If its not possible to do this with arrays in LabVIEW I will look for a different way to do it.
I'm pretty sure I got most of the rest of the code down except for an unfinished part here and there. Its just my issue with the arrays not working as I want them too.
I expected the array to retain its previously inputted data for index n-1 when index n is inputted. And only to be replaced once the index has come back to that specific point.
Instead its like a new array is initialized every time a new index is input.
download link for the VI
What you want to do:
Transport the content of the modified array into the next iteration of the WHILE loop.
What happens:
On each iteration, the content of the array is the same. It is the content of the initial array you created outside.
To solve this, right-click the orange square on the left border of the loop, and make it a "shift register". The symbol changes, and a similar symbol appears on the right border. Now, wire the modified array to the symbol on the right. What flows out into that symbol on the right, comes in from the left symbol on the next iteration.
Edit:
I have optimized your code a little. There is a modulo function, and an IF clause can handle ranges. ..3 means "values lower or equal 3". The next case is "Default", the next "7..". Unfortunately, this only works for integers. Otherwise, one would use nested IF clauses with the < comparator or similar.

I have 10 variables and I have to fill them with either 1 or -1 , how do I list out all the possible cases?

I'm working on a math problem dealing with arrangements of Males and Females. There are 5 couples and we know that each couple sit adjacent to each other , every male is seated opposite to a female . If I am to represent male or female with either +1 or -1 and want to find the list of all possible outcomes and print them, how do I go about it?
Do I have to make use of 10 FOR loops? Is there a simpler way in which I could perhaps store the results in an array?
P.S. The math question isnt limited to what I have written here, I want to solve it by myself(sorry) but I would like to understand the issue I have highlighted above. If possible show me a small snippet to point me in the right way.
The most "efficient" or "simple" way to generate all ways to get 10 numbers containing 1 or -1 depends on the language. Python has a very simple and quick way. Here is an expression that creates a generator that does this. First execute the command
import itertools
to get what you need into your namespace, then the expression is
itertools.product((1, -1), repeat=10)
For example, to work on all those tuples of 10 numbers you could do:
import intertools
for mytuple in itertools.product((1, -1), repeat=10):
# Process mytuple
If you are not familiar with Python, each "tuple" that is created for a cycle of the loop is basically equivalent to an array.
Check out the Power Set to generate all possible subsets of a set.
There are many ways to generate this, but you stated that you want to figure it out.
This link from math.stackexchange.com seems relevant
If order doesn't matter, there are eleven solutions: one for each possible number of -1 in the output list. To list these, loop from 0 to 10 (inclusive) and then loop from 1 to 10 (inclusive), first printing a number of -1 equal to the value of the outer counter, then printing 1 until the end of the inner loop.
If order does matter, a simple recursive solution is to start with an empty buffer and position zero. Then, at each level, add -1 and 1, in turn, to the current position, recursively filling the remainer of the buffer by incrementing the position in each call. When the position is equal to the buffer length (hence the buffer is full) you can print the buffer's content and terminate the recursion. This is O(n2^n) time and O(n) space, where n is the buffer length. If stack overflow becomes a concern, consider rewriting this using iteration and a stack to explicitly manage the call stack yourself.

replacing r variables with multiple or different values

I have a dataframe (raw) that can have one variable (iv1) with NA's in it. I want to replace the NA with different random values from the distribution of existing scores within (iv1), not one single value. the sample size (n) can be anything - 100 to 1000.
I save the distribution to a new data frame (dbmi) because I want to keep raw and dbmi separate, and calculate the mean and SD of the existing values of iv1 within dbmi. The following code works but replaces all of the NA's with just one value. I think I need to set up a for loop? Some kind of loop that finds the next occurrence of an NA and runs the new 'rnorm' value and sticks it in and goes to the next and does it again etc etc but I cant figure out how to do that. Any help?
dbmi<-raw
attach(dbmi)
rawmean<-mean(dbmi$iv1,na.rm=TRUE)
rawsd<-sd(dbmi$iv1,na.rm=TRUE)
for (i in 1:n){
dbmi$iv1[is.na(dbmi$iv1)]<-rnorm(1,rawmean,rawsd)
}
I actually solved my own problem. I set up the variable locations [i] that had the NA's into a variable called 'pull', then I just created a new stream into a variable called 'new' I used this code to substitute.
dbmi<-raw
attach(dbmi)
rawmean<-mean(dbmi$iv1,na.rm=TRUE)
rawsd<-sd(dbmi$iv1,na.rm=TRUE)
new<-rnorm(num,rawmean,rawsd)
for (i in 1:n){
dbmi$iv1[pull]<-new
}

Creating an XY graph in labview which would store previous values

Link to the vi: see xy_plot_problem_withcase
In the attached vi (xy_plot_problem_updated.vi) I am able to get 3 individual values x, y and z in an array, element 0 being x, element 1 being y and element 2 being z.
These three values come for every iteration of the outer while loop. I would like to store all generated x values into one array and same with y and z so I can use the final arrays to generate one final graph.
The outer while loop runs 30 times and I would like to record the 30 different values generated at index 0 in a separate array. I tried using a shift register, build array etc but its just replacing element 1 (of the new array) with the newest element generated (They are not getting accumulated).
I have encountered this problem while designing for a system which records 3 different readings for every 5 degree increase in temperature. I want to be able to plot the acquired values against the current temperature. Hence, the outer while loop is actually a case statement which gets triggered every time the temperature goes up by 5 degrees.
I have also attached the main VI too alongside (final.vi).
Any help appreciated!!
Thanks in advance!!!
In your final.vi you have a while loop, you should move everything in the case into the while loop. My advise for you would be to look at the LabVIEW fundamentals on data flow and on shift registers.
In your code you were resetting the shift register in the while loop every iteration.
Try to clean up your code and use the executing highlighting function (the light bulb).

programming of 3 x 7

I have been trying to program the 3x 7 trick. The full code is available here: https://codereview.stackexchange.com/questions/9419/programming-of-3-x-7-trick.
I am stuck at step 5.
I have this error when i type in the value for the row.
Not sure what the error is. Need some guidance.
step 6:
That error comes from the back_to_array function, where you have a typo in the condition of the inner loop. It should be j < numRows instead of i < numRows.
The main issue is the mistake with i < numRows instead of j < numRows.
As per requested, here are some other modifications you could and should implement:
Write printf("%8i", ... instead of printf("%i\t", ..., since the latter is likely to spread out the numbers unevenly.
Sanitize your input. Right now, you can make the program crash by inputting strange values. (Also, give the user a hint about whether to use the values 0, 1, 2 or 1, 2, 3.)
Right now, you're not shuffling row 0 and column 0. For instance, you start with column 6 and go through the columns one by one, but you stop as soon as you reach 0, before entering the loop again.
There's a problem where you quite often notice that the same numbers occur together on the same row. I believe, although I'm not completely sure, that the problem is that you're sorting the rows. The point of placing the selected row in the middle of the deck all the time, is to make the selected card move towards the center. If you sort the row, you allow the card to move away from the center. I commented out the sorting and couldn't notice the problem anymore. Is there a reason why you sort the rows?
Regarding design: Personally, I'd not display a shuffled array and wait for a keystroke before beginning. Instead, I'd write the instructions and immediately ask the player to enter a number. I always tried to enter a number already after the first array had been displayed. Very annoying. :)

Resources