MLT 3 ENCODING question is causing confusion - mlt

How does the expression mod(cumsum([1 0 1 1 0 0 1]),4) perform MLT-3 encoding?
Currently trying this out in Octave to get a better understanding but can't seem to wrap my head around it.

Related

C++/CLI Load variables from a .txt file using a special structure

Rather long question, and quite long story so I'll try to be as quick and precise as I can. I've written a program which allows the user to create questions for a quiz, and those options be exported into a .txt file with the below structure:
#
Level: 1
Ref: testRef
Question: test question
A: test A
B: test B
C: test C
D: test D
Ans: A
APerc: 100
BPerc: 0
CPerc: 0
DPerc: 0
Phone Answer: Right, I know this. The answer is 100% A. Good luck!
50/50: B
50/50 Percentage 1: 100
50/50 Percentage 2: 0
Force: True
!
Where
# indicates the start of the question,
Level is the part of the quiz this question is worth (the higher it is, the more difficult it is),
A, B, C and D are the alternative answers,
ans is the correct answers,
APerc to 50/50 Percentage 2 are all info for assists to the user to answer the question if they use them,
the value of force is whether this question should appear definitively or not, since their may be more than once question with the same level
and ! indicates the end of a question.
Now, in terms of code what would be the easiest way to read the whole file and identify each of these individual questions with the aforementioned delimiters?
Would it be good to use something like StreamReader? I'd rather read the whole file, then identify if any questions have been forced, then randomly pick 15 questions, either at the start of runtime or during the reveal of the next question, with different levels, possibly passing the value of the current level to the read function and then store all the question info in variables.

How do I compare of two arrays in matlab? [duplicate]

This question already has answers here:
Comparing two matrices in Matlab
(6 answers)
Closed 8 years ago.
i've got an array like x, i want to do some works on it and put result in the new array y. then i should compare this two. if they are the same by a thershold(i.e they could be a little different) that's ok and algorithm ends otherwise i should continue the iteration
the problem is comparing these two.
they are a two 2d array with unknown elements.
i've done two different way but none of them where ok:
first way:
d = x - y
if d < 5
disp('end')
end
and so on
but it does not work well,honestly it doesn't work at all
the other way which i used is:
isequal(x,y)
while they are the same it will return 0 but if they are not and even with a little difference the result will be 1 and it is not ok cause as i said algorithm should consider a litlle difference and stop the iteration
what should i do?
If 5 is an OK threshold, then this should work:
d=abs(x-y);
if all(d<5)
disp('end')
end
If you don't know what the threshold is, then that's a very different question. Determining a sensible threshold is dependant on your application, and is often a trade-off - there may not be a "right" answer if your data is variable. Look into some basic statistics - the zscore command may be a useful start.
One other way to inspect the difference vector is to use "find()" function in MATLAB. As Nolan, I think you better use the absolute value of the difference.
idx = find(abs(a-b)>threshold) will give you the indices that exceed the threshold. If null, then you terminate your iterations.

Unit testing a binary format reader in C

I am writing a C library that reads a binary file format. I don't control the binary format; it's produced by a proprietary data acquisition program and is relatively complicated. As it is one of my first forays into C programming and binary file parsing, I am having a bit of trouble figuring out how to structure the code for testing and portability.
For testing purposes, I thought the easiest course of action was to build the library to read an arbitrary byte stream. But I ended up implementing a stream data type that encapsulates the type of stream (memstream, filestream, etc). The interface has functions like stream_read_uint8 such that the client code doesn't have to know anything about where the bytes are coming from. My tests are against a memstream, and the filestream stuff is essentially just a wrapper around FILE* and fread, etc.
From an OOP perspective, I think this is a reasonable design. However, I get the feeling that I am stuffing the wrong paradigm into the language and ending up with overly abstracted, overly complicated code as a result.
So my question: is there a simpler, more idiomatic way to do binary format reading in plain C while preserving automated tests?
Note: I realize that FILE* is essentially an abstract stream interface. But the implementation of memory streams (fmemopen) is non-standard and I want Standard C for portability.
What you described is a low-level I/O functionality. Since fmemopen() is not 100% portable (off Linux, it creaks, I suspect), then you need to provide yourself with something portable that you write that is sufficiently close that you can use your surrogate functions (only) when necessary and use the native functions when possible. Of course, you should be able to force the use of your functions even in your native habitat so that you can test your code.
This code can be tested with known data to ensure that you pick up all the characters in the input streams and can faithfully return them. If the raw data is in a specific endian-ness, you can ensure that your 'larger' types — hypothetically, functions such as stream_read-uint2(), stream_read_uint4(), stream_read_string() etc — all behave appropriately. For this phase, you don't really need the actual data; you can manufacture data to suit yourself and your testing.
Once you've got that in place, you will also need to write code for reading the data with the larger types, and ensuring that these higher level function actually can interpret the binary data accurately and invoke appropriate actions. For this, you finally need examples of what the format supplied; up until this phase you probably could get away with data you manufactured. But once you're reading the actual files, you need examples of those to work on. Or you'll have to manufacture them from your understanding and test as best you can. How easy this is depends on how clearly documented the binary format is.
One of the key testing and debugging tools will be canonical 'dump' functions that can present data for you. The scheme I use is:
extern void dump_XyzType(FILE *fp, const char *tag, const XyzType *data);
The stream is self-evident; usually it is stderr, but by making it an argument, you can get the data to any open file. The tag is included in the information printed; it should be unique to identify the location of call. The last argument is a pointer to the data type. You can analyze and print that. You should take the opportunity to assert all validity checks that you can think of, to head off problems.
You can extend the interface with , const char *file, int line, const char *func and arrange to add __FILE__, __LINE__ and __func__ to the calls. I've never quite needed it, but if I were to do it, I'd use:
#define DUMP_XyzType(fp, tag, data) \
dump_XyzType(fp, tag, data, __FILE__, __LINE__, __func__)
As an example, I deal with a type DATETIME, so I have a function
extern void dump_datetime(FILE *fp, const char *tag, const ifx_dtime_t *dp);
One of the tests I was using this week could be persuaded to dump a datetime value, and it gave:
DATETIME: Input value -- address 0x7FFF2F27CAF0
Qualifier: 3594 -- type DATETIME YEAR TO SECOND
DECIMAL: +20120913212219 -- address 0x7FFF2F27CAF2
E: +7, S = 1 (+), N = 7, M = 20 12 09 13 21 22 19
You might or might not be able to see a value 2012-09-13 21:22:19 in there. Interestingly, this function itself calls on another function in the family, dump_decimal() to print out the decimal value. One year, I'll upgrade the qualifier print to include the hex version, which is a lot easier to read (3594 is 0x0E0A, which is readily understandable by those in the know as 14 digits (E), starting with YEAR (the second 0) to second (A), which is certainly not so obvious from the decimal version. Of course, the information is the in the type string: DATETIME YEAR TO SECOND. (The decimal format is somewhat inscrutable to the outsider, but pretty clear to an insider who knows there's an exponent (E), a sign (S), a number of (centesimal) digits (N = 7), and the actual digits (M = ...). Yes, the name decimal is strictly a misnomer as it uses a base-100 or centesimal representation.)
The test doesn't produce that level of detail by default, but I simply had to run it with a high-enough level of debugging set (by command line option). I'd regard that as another valuable feature.
The quietest way of running the tests produces:
test.bigintcvasc.......PASS (phases: 4 of 4 run, 4 pass, 0 fail)(tests: 92 run, 89 pass, 3 fail, 3 expected failures)
test.deccvasc..........PASS (phases: 4 of 4 run, 4 pass, 0 fail)(tests: 60 run, 60 pass, 0 fail)
test.decround..........PASS (phases: 1 of 1 run, 1 pass, 0 fail)(tests: 89 run, 89 pass, 0 fail)
test.dtcvasc...........PASS (phases: 25 of 25 run, 25 pass, 0 fail)(tests: 97 run, 97 pass, 0 fail)
test.interval..........PASS (phases: 15 of 15 run, 15 pass, 0 fail)(tests: 178 run, 178 pass, 0 fail)
test.intofmtasc........PASS (phases: 2 of 2 run, 2 pass, 0 fail)(tests: 12 run, 8 pass, 4 fail, 4 expected failures)
test.rdtaddinv.........PASS (phases: 3 of 3 run, 3 pass, 0 fail)(tests: 69 run, 69 pass, 0 fail)
test.rdtimestr.........PASS (phases: 1 of 1 run, 1 pass, 0 fail)(tests: 16 run, 16 pass, 0 fail)
test.rdtsub............PASS (phases: 1 of 1 run, 1 pass, 0 fail)(tests: 19 run, 15 pass, 4 fail, 4 expected failures)
Each program identifies itself and its status (PASS or FAIL) and summary statistics. I've been bug hunting and fixing a bug other than the ones I found coincidentally, so there are some 'expected failures'. That should be a temporary state of affairs; it allows me to claim legitimately that the tests are all passing. If I wanted more detail, I could run any of the tests, with any of the phases (sub-sets of the tests which are somewhat related, though the 'somewhat' is actually arbitrary), and see the results in full, etc. As shown, it takes less than a second to run that set of tests.
I find this helpful where there are repetitive calculations - but I've had to calculate or verify the correct answer for every single one of those tests at some point.

1 = false and 0 = true?

I came across an is_equals() function in a c API at work that returned 1 for non-equal sql tables (false) and 0 for equal ones (true). I only realized it after running test cases on my code, one for the positive example and one for the negative and they both failed which at first made little sense. The code in the API does not have a bug as the output was recorded correctly in its documentation.
My questions - are there upside down worlds / parallel universes / coding languages where this logical NOTing is normal? Isn't 1 usually true? Is the coder of the API making an error?
It is common for comparison functions to return 0 on "equals", so that they can also return a negative number for "less than" and a positive number for "greater than". strcmp() and memcmp() work like this.
It is, however, idiomatic for zero to be false and nonzero to be true, because this is how the C flow control and logical boolean operators work. So it might be that the return values chosen for this function are fine, but it is the function's name that is in error (it should really just be called compare() or similar).
This upside-down-world is common with process error returns. The shell variable $? reports the return value of the previous program to execute from the shell, so it is easy to tell if a program succeeds or fails:
$ false ; echo $?
1
$ true ; echo $?
0
This was chosen because there is a single case where a program succeeds but there could be dozens of reasons why a program fails -- by allowing there to be many different failure error codes, a program can determine why another program failed without having to parse output.
A concrete example is the aa-status program supplied with the AppArmor mandatory access control tool:
Upon exiting, aa-status will set its return value to the
following values:
0 if apparmor is enabled and policy is loaded.
1 if apparmor is not enabled/loaded.
2 if apparmor is enabled but no policy is loaded.
3 if the apparmor control files aren't available under
/sys/kernel/security/.
4 if the user running the script doesn't have enough
privileges to read the apparmor control files.
(I'm sure there are more widely-spread programs with this behavior, but I know this one well. :)
I suspect it's just following the Linux / Unix standard for returning 0 on success.
Does it really say "1" is false and "0" is true?
There's no good reason for 1 to be true and 0 to be false; that's just the way things have always been notated. So from a logical perspective, the function in your API isn't "wrong", per se.
That said, it's normally not advisable to work against the idioms of whatever language or framework you're using without a damn good reason to do so, so whoever wrote this function was probably pretty bone-headed, assuming it's not simply a bug.
It may very well be a mistake on the original author, however the notion that 1 is true and 0 is false is not a universal concept. In shell scripting 0 is returned for success, and any other number for failure. In other languages such as Ruby, only nil and false are considered false, and any other value is considered true, so in Ruby both 1 and 0 would be considered true.
Simple Answer
0 = false
1 = true
I'm not sure if I'm answering the question right, but here's a familiar example:
The return type of GetLastError() in Windows is nonzero if there was an error, or zero otherwise. The reverse is usually true of the return value of the function you called.
Generally we think that 0 is false and 1 is true

Nested Loop / Loop Control tutorial [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I'm looking for a good tutorial on writing and designing loops. I understand the basics of loops but nested loops give me a lot of trouble. To give you and idea, the following pattern below was kind of difficult for me to figure out.
1
12
123
1234
12345
123456
Loops
A loop is a construct that enables a set of instructions to be executed more than once.
There are several loop constructions:
zero or more
These loops have the check at the begining of an iteration and as such will be executed 0 or more times. A while loop is an example.
one or more
These loops have the check at the end of the iteration and as such will be executed at least once. A do while loop is an example.
Loops with counters
These loops have a counter that counts from a certain number to an other number. The number can be used inside the loop (for example to access a field of an array).
Loops with an iterator
These loops use an iterator to loop through a certain structure.
Endless loops
These loops have no end. But of course nothing is forever, so the loop often contains a hidden mechanism.
Nested loops
If you understand single loops, nested loops can be difficult. But you need to focus on one loop at a time.
Lets take your example:
1
12
123
1234
12345
123456
Ok, lets first look at the lines.
The first line has a single 1
The second line counts from 1 to 2
The third line counts from 1 to 3
...
Generally: the n th line counts from 1 to n.
Great, no we have the individual line. But let's now look at all lines.
the first has n=1
the second has n=2
the third has n=3
...
Hm, so we can use the loop counter of the outer loop as the n in the inner loop:
for n = 1 to 6
s = ''
for i = 1 to n // use the loopcounter of the outer loop
s = s + char(i)
end for
out s
end for
Check out:
http://mathbits.com/mathbits/compsci/looping/nested.htm
http://tldp.org/LDP/abs/html/nestedloops.html
http://www.actionscript.org/resources/articles/5/1/The-power-of-nested-loops/Page1.html
In general (language-neutral) terms, the basic logic is quite straightforward. Where it can get more complex is if an inner loop terminates early & the manner of the break. It may cause the outer loop to move to the next value, or it may completely exit the outer loop as well.
The best way to learn this is to try out different cases to see how they behave, and read up on the ways to exit from loops.
How about these:
Nested Loops
The Power of Nested Loops
or on YouTube "SQL Joins, nested loops and all that in less than 6 minutes" at http://www.youtube.com/watch?v=SmDZaH855qE
I don't remember seeing any "loop design" centred tutorials when I was learning to program.
You will get a grasp of loops if you just start tackling on different problems and algorithms. Look for matrix problems, you will need nested loops there for instance...
I'm unaware of any tutorials on this subject, but I suggest you try Google. Also, the fact that you were able to figure out your example probably means that you don't need a tutorial as much as you need practice. Nested loops are somewhat mind-warping when you first encounter them. You might also want to look for references/tutorials pertaining to recursion, which is a related concept. Remember, practice makes perfect!
Check out the MIT Course Material. Also consider getting a Safari subscription, a cheap way of getting some good learning books.
This MIT course points to Loops on the python wiki.
I found that working it out on paper, listing the variables helps in learning how this works.
declare
s varchar2(10);
begin
for n in 1..5 loop
s:='';
for i in 1..n loop
s:=s||(i);
end loop;
dbms_output.put_line(s);
end loop;
end;

Resources