How to Access a Moving Portion of an Array - c

I'm at a loss for what to do in my program (written in C). There is a large matrix of numbers (an image) that I am processing. The processing happens one line at a time, with reference to the previous line, so I only need to access two lines of numbers at a time.
Originally, I tried a 2 by X array but once I save the information for the third line, the array is upside down with the third line of the image in the first row of the array and the second line of the image in the second row of the image.
Is there a better way to correct this other than simply copying the second row of the array to the first row? Maybe it wouldn't be so bad, but I would imagine doing that for every other line of the image would be expensive. Should I use two pointers on the image instead?
I apologize if this is a common thing that can be easily looked up but I couldn't figure out how to begin looking. If anyone needs clarification, let me know. Thank you very much!
Diagram of what numbers I need access to:
http://www.gliffy.com/go/publish/5968966

I suppose that you are processing the image as you read it, or as you decompress it, or some such, for if you already had the whole thing in memory in usable form then you would just use that.
I see two reasonably good alternatives:
Instead of hard-coding the indices of the earlier and later lines in your 2 by x array, use variables to track which row contains which line.
Use a 1-D array for each line, and use pointers to track which one contains the current line and which one contains the previous line.
(Though really, those boil down to pretty much the same thing.) Either way, you can avoid needless copying.

Let's assume you have:
struct rbg_t bitmap[X][Y];
To get a window of dimensions X by 2, it is like this:
struct rgb_t *first_line = bitmap[0], *second_line=bitmap[1];
Then you can process the two lines like so:
for(int i=0;i<X;++i)
{
do_work(first_line[i], second_line[i]);
}
To shift the window down by one, you can do this:
first_line+=sizeof(struct rgb_t)*X;
second_line+=sizeof(struct rgb_t)*X;
Where X is the width of the bitmap

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.

Most efficient way to add a constant to a column of an array in LabVIEW?

I want to add a constant to the second column of an array.
I do this as shown below:
Where for illustration the values are as follows:
What is the most efficient way of adding a constant to an array column?
With a question about efficiency you should supply number. For anything lower than a 1000 x 1000 2D array I can't measure the difference. Usually it is best to simply test it.
Here the code for testing (same answer as crossrulz)
With a 10000 x 10000 array option 2 becomes about 10 times faster.
One comment unless you are in a very high demanding situation, readability is usually preferred over efficiency. In my opinion option 2 is more readable since it has no for loop and the constant is presented as a constant instead of an array.
But you can get more efficient than that by using the In Place Element structure. The image below shows two different ways to add 5 to a column. The second one avoids making a memory copy of the entire array. Indexing out a column of an array with Index Array and then modifying it requires a shift of underlying memory format, even though the array is going to be put back in the Replace Array Subset. The In Place Element structure gives enough context to LabVIEW for it to recognize that the Add can be done without data copies.
Index Array to get the second column, add your constant, and then Replace Array Subset to replace the second column.

How can I printf first in columns then in lines?

maybe this question seems stupid... I would like to fprintf a dataset formatted in rows and columns. I know the procedure so far
for(i=0;i<number_of_rows;i++)
{
for(j=0;j<number_of_columns;j++)
fprintf(file,"%g\t",array[i][j]);
fprintf(file,"\n");
}
What essentially does this code is to fill up first the rows and then the columns. I have datasets that are created by columns. Thus, I want to fill up every column before I pass to the next one. I don't know before the runtime the length of the output data in order to make a proper 2D array. And I need to print out first a whole column, then the other. How can I do this?
And I need to print out first a whole column, then the other. How can I do this?
That would involve appending to a previously printed line. And, as you may know, adding content into a file involves rewriting the entire rest of the file. You can probably guess that would be incredibly inefficient and also complicated to implement.
I don't know before the runtime the length of the output data in order to make a proper 2D array
If your problem is the length of the data, then I assume that by proper 2D array you mean that you intend to pad each cell of a column to be the same width.
In that case, I recommend not to waste time on the idea of writing column first. Instead, calculate the widths at runtime. First sprintf into a matrix of strings and calculate the length of each cell. Then print the matrix of strings using the calculated lengths.

What is the best way to count the number of times a value occurs in an array?

I have been given an assignment to write a program that reads in a number of assignment marks from a text file into an array, and then counts how many marks there are within particular brackets, i.e. 40-49, 50-59 etc. A value of -1 in the text file means that the assignment was not handed in, and a value of 0 means that the assignment was so bad that it was ungraded.
I could do this easily using a couple of for loops, and then using if statements to check the values whilst incrementing appropriate integers to count the number of occurences, but in order to get higher marks I need to implement the program in a "better" way. What would be a better, more efficient way to do this? I'm not looking for code right now, just simply "This is what you should do". I've tried to think of different ways to do it, but none of them seem to be better, and I feel as if I'm just trying to make it complicated for the sake of it.
I tried using the 2D array that the values are stored in as a parameter of a function, and then using the function to print out the number of occurences of the particular values, but I couldn't get this to compile as my syntax was wrong for using a 2D array as a parameter, and I'm not too sure about how to do this.
Any help would be appreciated, thanks.
Why do you need a couple for loops? one is enough.
Create an array of size 10 where array[0] is marks between 0-9, array[1] is marks between 10-19, etc. When you see a number, put it in the appropriate array bucket using integer division, e.g. array[(int)mark/10]++. when you finish the array will contain the count of the number of marks in each bucket.
As food for thought, if this is a school assignment, you might want to apply other things you have learned in the course.
Did you learn sorting yet? Maybe you could sort the list first so that you are not iterating over the array several times. You can just go over it once, grab all the -1's, and spit out how many you have, then grab all the ones in the next bracket and so on.
edit: that is of course, assuming that you are using a 1d array.

Storing strings of different sizes in a MATLAB array?

I want to be able to store a series of strings of different sizes such as
userinput=['AJ48 NOT'; 'AH43 MANA'; 'AS33 NEWEF'];
This of course returns an error as the number of columns differs per row. I'm aware that all that is needed for this to work is adequate spaces in the first and second rows. However I need to be able to put this into an array without forcing the user to add these spaces on his/her own. Is there a command that allows me to do this? If possible I'd also like to know why this problem doesn't arise with numbers e.g.
a=[1; 243; 23524];
You cannot do this with standard Matlab arrays. A string is really just a vector of characters in Matlab. And you cannot have a matrix with rows of different lengths.
You can, however, use a cell array:
userinput={'AJ48 NOT'; 'AH43 MANA'; 'AS33 NEWEF'};
disp(userinput{1});
Be aware that there are many situations where cell arrays don't work like normal arrays.
To just answer to your last part of your question; simply because strings may be variable length but numbers (in Matlab) are fixed length. It's one of the main ideas of arrays to let them hold only fixed sizes entities (for example because the need of efficient look up), see more on the topic here.

Resources