Create an "array style" countdown in c - c

What I want to know is, if it's possible to create a countdown in c, BUT have a condition for when it hits an "unsual" piece of data in the array. I'll explain better with examples.
This is also similar to: Read ahead in an array to predict later outcomes in C
however, it was poorly worded. So, I am rewording this question.
Ex: The array is an integer array with : 0 0 0 0 1 0 0 0 0 0 0 0 0 0 5 0 0 0 0 0 2
So, when its zero, don't do anything.
However, when it's non-zero, display the text associated with the number (according to some condition).
With pseudocode it'd be something like this:
if 0 dont do anything ====> within this countdown till next non-zero
if != 0 then display text asociated
reset countdown till next non-zero.
Is there any way this can be achieved? Basically, this would mean you could predict or read ahead in the array. Any help would really be appreciated!

Related

How to get data from specific input file format skipping line with scanf [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I have a .txt file that contains info like this:
9:0
B1 0 0 0 0 0
B2 0 0 0 0 0
B3 4 5 0 0 0
B4 1 2 3 0 0
9:1
B1 0 0 0 0 0
B2 0 0 0 0 0
B3 4 5 0 0 0
B4 1 2 3 0 0
9:2
B1 0 0 0 0 0
B2 0 0 0 0 0
B3 4 5 0 0 0
B4 1 2 3 0 0
(...)
As you can see, the format is a line with the time, and the next four lines is the identifier of "B" followed by five elements of an array.
I can easily read the whole file, looping through the whole file (I know how many lines the file has) using first of all scanf to get the date, next another scanf to read the "B" plus the identifier, and loop four times with scanf again to get the integers of the array and go back to get the time.
This works fine, but (again) as you can see, there's a lot of zeros in the array space, and it would be a lot faster if I check the first element and if it is zero, then skip and read the next box, so I did it and used the break; statement, but the problem is that using break will ruin the structure of my loop, storing in the time variable a B identifier sometimes.
I'm wondering if there's any other way to skip the zeros when I find one, and jump to the next B identifier, i.e. after reading 9:0, i get B1 and then read the first zero, so skip this line and get 'B2' until I find a non-empty array.
If anyone could help me, please!
I think a reasonably fast solution might be to read the entire line into a static char array, skipping to the next line if the 4'th character is 0, and using sscanf to read the values otherwise.
My reasoning here is that the work of fscanf is split between (1) reading in a string from a file, and (2) parsing that string into numbers, and moving them into the provided variables. However, I know IO operations can be pretty slow, so here is another alternative that does slightly less IO.
Use fscanf to read in only the first two tokens of a line (namely, the B_ and the first number), and use fseek to skip to the next line if the first number is 0. I'm not super confident that this will be faster, but its something you could try.

How do you calculate the number of elements in a jagged array in F#?

I am new to F# and haven't found the answer to this anywhere. I am creating a jagged array that can hold 10 rows and 10 columns each with an increasing number of elements. The code I used for the array creation and printing is as follows:
let jagged = [| for a in 1 .. 10 do yield [| for a in 1 .. a do yield 0 |] |]
let mutable len = 0;
for arr in jagged do
for col in arr do
len <- (len + 1)
printf "%i " col
printfn "";
printfn "%i" len
The above code gives the following output
0
0 0
0 0 0
0 0 0 0
0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
55
Currently, I am calculating the number of elements manually but would like to know if there is a better way to do so.
If you want to calculate the length of a single array, you could use Array.length. But what you have is an array of arrays of different lengths, and you want to calculate the sum of their sizes. Rather than just give you the answer, I'll show you how you could use https://fsharpforfunandprofit.com/posts/list-module-functions/ (a site by Scott Wlaschin that's a really terrific resource, BTW) to find the answer yourself. This page presents a series of questions to help you find the functions you're looking for: starting from question 1, you move to other questions and eventually to a list of useful functions.
Question 1 on that page is, "What kind of collection do you have?" The choices are "I don't have a collection and I want to create one", or "I have one collection I want to work with", or several other choices where you have two or three or more collections. Here, we have one collection we want to work with, so the page directs us to question 9.
Question 9 on that page has a bunch of choices I won't repeat here, but one of them is "If you want to aggregate or summarize the collection into a single value". That sounds like what we want: we want the sum of the lengths of the sub-arrays. So we go to section 14, which has a bunch of functions we could use. And halfway down the list is sum and sumBy. Those sound intriguing. The sum function "returns the sum of the elements in the collection"... well, no, that won't work, because our array contains arrays, not numbers. But the sumBy function "returns the sum of the results generated by applying the function to each element of the collection." And we know there's a function for finding the length of a single array: Array.length. (The page talks about functions that work on lists, but pretty much any function that works on lists has a corresponding function that works on arrays and a similar corresponding function that works on sequences. The few exceptions are for things like how you can have infinite sequences, but not infinite arrays or lists, so there's a Seq.initInfinite function but there's no Array.initInfinite or List.initInfinite function).
So now that we've found that, we just need to write it.
let lengthOfJaggedArray arr = arr |> Array.sumBy Array.length
And that's it. Instead of calculating the length by hand via two nested for loops, there's a one-line solution that's quite simple and uses built-in functions. All you needed to do was know what functions are available — and since the entire list of available array/list/seq functions can be a little daunting when you're new to F#, Scott Wlaschin has made a very useful resource to help make it a bit less daunting.

Generating a matrix to describe a two-dimensional feature

Let's say I have a vector A = [-1,2];
Each element in A is described by the actual number and sign. So each element has a 2 dimensional feature-set.
I would like to generate a matrix, in this case 2x2 where the columns correspond to the element, and rows correspond to the presence of a feature. The presence of a feature is described by 1's and 0's. So, if an element is positive, it is 1, if the element is the number 1, then the result is 1 as well. In the case above I would get:
Element 1 Element 2
Is this a 1? 1 0
Is this a positive number? 0 1
What is the smartest way to go about accomplishing this? Obviously if statements would work, but I feel that there should be a faster, much smarter way of going about this. I am coding this in matlab by the way, and I would appreciate any help.
#Benoit_11's solution is a fine one. Here's a similar but maybe simpler solution. You could try both and see which is faster if you care about speed.
features = [abs(A) == 1; A > 0];
this assumes A is a row vector in order to get the output in the format you specified.
Simple way using ismember for the first condition and logical operation for the 2nd condition. ismember outputs a logical array which you can plug into the output you need (here called DescribeA; and likewise when you check for values greater than 0 using the > operator.
%// Test array
A = [-1,2,1,-10,5,-3,1]
%// Initialize output
DescribeA = zeros(2,numel(A));
%// 1st condition. Check if values are 1 or -1
DescribeA(1,:) = ismember(A,1)|ismember(A,-1);
%// Check if they are > 0
DescribeA(2,:) = A>0;
Output in Command Window:
A =
-1 2 1 -10 5 -3 1
DescribeA =
1 0 1 0 0 0 1
0 1 1 0 1 0 1
I feel there is a smarter way for the 1st condition but I can't seem to find it.

find largest rectangle not (necessary) aligned with image boundary in binary matrix

I am using this solution to find rectangles aligned with the image border in a binary matrix. Suppose now I want to find a rectangle that is not aligned with the image border, and I don't know its orientation; what would be the fastest way to find it?
For the sake of the example, let's look for a rectangle containing only 1's. For example:
1 1 1 1 0 0 0 0 0 1 0 0 1 1 1
0 1 1 1 1 1 0 0 0 1 0 0 1 1 0
0 0 0 1 1 1 1 1 0 1 0 0 1 0 0
0 0 0 0 0 1 1 1 1 1 0 0 0 0 0
0 0 0 0 0 0 0 1 1 1 1 1 0 0 0
0 0 0 0 0 0 0 0 0 1 1 1 1 1 0
Then the algorithm described in the solution I described above would only find a rectangle of size 6 (3x2). I would like to find a bigger rectangle that is tilted; we can clearly see a rectanble of at least size 10 or more...
I am working in C/C++ but an algorithm description in any language or pseudo-code would help me a lot.
Some more details:
there can be more than one rectangle in the image: I need the biggest only
the rectangle is not a beautiful rectangle in the image (I adapted my example above a little bit)
I work on large images (1280x1024) so I'm looking for the fastest solution (a brute-force O(n³) algorithm will be very slow)
(optional) if the solution can be parallellized, that is a plus (then I can boost it more using GPU, SIMD, ...)
I only have a partial answer for this question, and only a few thoughts on complexity or speed for what I propose.
Brute Force
The first idea that I see is to use the fact that your problem is discrete to implement a rotation around the center of the image and repeat the algorithm you already use in order to find the axis aligned solution.
This has the downside of checking a whole lot of candidate rotations. However, this check can be done in parallel since they are indepedant of one another. This is still probably very slow, although implementing it (shouldn't be too hard) and would provide a more definite answer to the question speed once parallelized.
Note that your work-space being a discrete matrix, there is only a finite number of rotation to browse through.
Other Approach
The second solution I see is:
To cut down your base matrix so as to separate the connected components [1] (corresponding to the value set you're interested in).
For each one of those smaller matrices -- note that they may be overlapping depending on the distribution -- find the minimum oriented bounding box for the value set you're interested in.
Still for each one of those, rotate your matrix so that the minimum oriented bounding box is now axis-aligned.
Launch the algorithm you already have to find the maximum axis-aligned rectangle containing only values from your value set.
The solution found by this algorithm would be the largest rectangle obtained from all the connected components.
This second solution would probably give you an approximation of the soluiton, but I believe it might prove to be worth trying.
For reference
The only solutions that I have found for the problem of the maximum/largest empty rectangle are axis-aligned. I have seen many unanswered questions corresponding to the oriented version of this problem on 2D continuous space.
EDIT:
[1] Since what we want is to separate the connected component, if there is a degree of overlap, you should do as in the following example:
0 1 0 0
0 1 0 1
0 0 0 1
should be divided into:
0 0 0 0
0 0 0 1
0 0 0 1
and
0 1 0 0
0 1 0 0
0 0 0 0
Note that I kept the original dimensions of the matrix. I did that because I'm guessing from your post it has some importance and that a rectangle expanding further away from the boundaries would not be found as a solution (i.e. that we can't just assume there are zero values beyond the border).
EDIT #2:
The choice of whether or not to keep the matrix dimensions is debatable since it will not directly influence the algorithm.
However, it is worth noting that if the matrices corresponding to connected components do not overlap on non-zero values, you may choose to store those matrices "in-place".
You also need to consider the fact that if you wish to return as output the coordinates of the rectangle, creating a matrix with different dimensions for each connected component, this will force you to store the coordinates of your newly created matrix in the original one (actually, one point, say for instance the up-left one, should be enough).

Radial basis network character recognition

I want to develop a simple character recognition program by implementing a given neural network kind; a simple command line-type is enough.
The radial basis function neural network was assigned to me and I already studied the weight training, input-to-hidden-to-output procedures but I am still doubtful of in implementing it. My references are (1) and (2).
A simple one-dimensional array of a 10 by 10 binary object (that represents a character) is the input. For example, the array below
input = array(
0,0,0,1,1,1,1,0,0,0,
0,0,1,0,0,0,0,1,0,0,
0,1,0,0,0,0,0,0,1,0,
1,0,0,0,0,0,0,0,0,1,
1,1,1,1,1,1,1,1,1,1,
1,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,1 )
is the representation of the character "A":
0 0 0 1 1 1 1 0 0 0
0 0 1 0 0 0 0 1 0 0
0 1 0 0 0 0 0 0 1 0
1 0 0 0 0 0 0 0 0 1
1 1 1 1 1 1 1 1 1 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
I plan to take the total weight of the input and compare it to the training set as in the saved 1-D arrays of the other characters of the alphabet and the one with the closest is the prediction.
The problem is I tend to understand algorithms better if presented in a CLRS-manner or similar type as opposed to mathematical formula. I find it hard to understand the explanations in those two papers (which I find the easiest to read among others here in the Google search).
Can someone point me to a friendly algorithm for a RBNFF that takes in an array and produces an output of weights? If not, a paper that explains this in Layman's manner would be appreciated.
Training
For what I could find there is no "one right way" to train them.
The simplest training I could find was by a composition of two algorithms
(Clustering) Taking the left part (input weights and RBFs) of the network and doing unsupervised clustering. There is a few things you can try out hard/soft and the number of clusters/RBFs.
Each cluster is a representation of a single RBF with the weights connecting to it.
How you go from having clusters to get rbf and rbf weights depends on what clustering you are using. (I can extend this if it's unclear)
(Neural Network) The solving the left out part of the original RBFNN from the last step by using the output from the clustering as input to an ordinary single layer neural network.
Probably easier to find these more primitive algorithms easily explained
EDIT
found some "pseudo"-code with explanations that might explain it all better (written in C#)
http://msdn.microsoft.com/en-us/magazine/dn532201.aspx
(Supposedly) working python code
https://github.com/andrewdyates/Radial-Basis-Function-Neural-Network

Resources