Is this the correct way to breaking branch into rungs? - plc

I am new to the the AB PLC programming and would like to know if this:
rung with branches is equivalent to this: separated into rungs
Thanks.

They are NOT Equivalent.
There are differences in the comparators (EQU) for each line (Network), while the first one only compares [50 = Wrq_Seq...], the other program compares with different values (50, 51 and 52).
There is also difference in MOV block at the end of the line, while the first one writes [60 > Wrq_Seq], the other writes different values for each situation (51, 52 and 60)

Related

How can I print the ascii value of an input in Brainfuck?

What I want to do is for a Brainfuck code to print out the ascii value of the input. For example, typing in an input of "a" will give an output of 97. The python equivalent of this is print(ord(input())). What I'm thinking is that once I get the input with the , command, I can split the input value's digits into separate cells, and then print each cell individually. What I mean by this is let's say you type in an input of a. The , command will store the ascii value of a in the first cell(cell 0), which is 97 in this case. Then I run some algorithm that will split the 97 into its individual digits. So, in this case, cell 1 will have a value of 0(because 97 has a hundred digit of 0), cell 2 will have a value of 9, and cell 3 will have a value of 7. Then we can add 48 to each of those cells(0 has an ascii value of 48) and print each cell individually, starting from cell 1(the hundreds place). The problem I'm facing is writing the digit separation algorithm. I can't seem to make it work. My idea is to subtract 100 from the original number until that number is less than 100 while keeping track of how many times 100 has been subtracted, then repeatedly subtract 10, and finally we are left with the ones place. But the problem with this idea is that I have no idea how to track if the number falls under 100 or 10. Any suggestions or ideas? Thanks for the help in advance.
What you are trying to implement is called "divmod". divmod is a function that divides two numbers (in your case positive integers) and stores the result and the remainder. Implementations for this in brainfuck exist: Divmod algorithm in brainfuck
Good luck!

Forcing MATLAB to use `single` precision as default?

Is there a way to force MATLAB to use single precision as default precision?
I have a MATLAB code, whose output I need to compare to C code output, and C code is written exclusively using floats, no doubles allowed.
Short answer: You can't.
Longer answer: In most cases, you can get around this by setting your initial variables to single. Once that's done, that type will (almost always) propagate down through your code. (cf. this and this thread on MathWorks).
So, for instance, if you do something like:
>> x = single(magic(4));
>> y = double(6);
>> x * y
ans =
4×4 single matrix
96 12 18 78
30 66 60 48
54 42 36 72
24 84 90 6
MATLAB keeps the answer in the lower precision. I have occasionally encountered functions, both built-in and from the FileExchange, that recast the output to be a double, so you will want to sprinkle in the occasional assert statement to keep things honest during your initial debugging (or better yet put the assertion as the first lines of any sub-functions you write to check the critical inputs), but this should get you 99% of the way there.
You can convert any object A to single precision using A=single(A);
The Mathworks forums show that
in your case: system-specific('precision','8'); should do it. Try this in the console or add at the top of your script.

Avoiding the use of for loops

I would like to get some help with how to avoid using for loops. I've seen several similar questions but have not been able to figure something specific to my needs. Currently I use loops and it is very bulky and messy. Below is the structure of the data and what I would like to achieve:
I have to index data from 1080 timepoints which come from variable mean_data which is a 1080x1 double. Some of the timepoints belong to specific task conditions and specific events that I am interested in. There are 3 conditions (cond1, cond2, cond3) and 4 task events (event1, event2, event3, event4). This information comes from variable params. Specially column 8 of params has the condition information (1, 2, 3 mean cond1, cond2, con3, respectively). The event information can be obtained from column 11 in params. Below is what I can do with loops:
for c=1:size(params,1)
if params(c,8)==1
cond1_event1(end+1,1)=mean([data(params(c,11)+3,1),data(params(c,11)+4,1)]);
cond1_event2(end+1,1)=mean([data(params(c,11)+6,1),data(params(c,11)+7,1)]);
cond1_event3(end+1,1)=mean([data(params(c,11)+8,1),data(params(c,11)+9,1)]);
cond1_event4(end+1,1)=mean([data(params(c,11)+10,1),data(params(c,11)+11,1)]);
elseif params(c,8)==2
cond2_event1(end+1,1)=mean([data(params(c,11)+3,1),data(params(c,11)+4+1,1)]);
etc.
elseif params(c,8)==3
cond3_event1(end+1,1)=mean([data(params(c,11)+3,1),data(params(c,11)+4,1)]);
etc.
end
end
The loops make it clear but it's just too long. Does anyone have any suggestions how to make this a bit more elegant? The output should yield 12 variables (3 condition x 4 events). Each variable is a nx1 double. Thank you.
You could simply use logical indexing, then use vertical concatenation.
idx = params(c, 8) == 1;
cond1_event1 = [cond1_event1; mean(...)];
Then repeat for the other conditions, or use a loop(1:3).

Matlab - Splitting a column into two (efficiently)

I had previously wrote some code to split 3 columns into 4, however the code was very inefficient and time consuming. As I am working with millions of rows it wasn't suitable. (Below is my previous code)
tline = fgetl(fid);
ID=tline(1:4);
IDN = str2double(ID);
Day=tline(6:8);
DayN = str2double(Day);
HalfHour=tline(9:10);
HalfHourN = str2double(HalfHour);
Usage=tline(12:end);
UsageN = str2double(Usage);
There must be a more efficient and quicker way of doing this?
Going back to basics, I have produced a x by 3 matrix. but require an x by 4 matrix
To show what I am trying to do, examining one row -
I am trying to change
1001 36501 1005
to
1001 365 01 1005
Any help would be much appreciated!
Edit:
The second column I am trying to divide into two, is always composed of 5 characters. I am trying to get the first 3 characters into their own column, likewise for the remaining characters.
What might take time in your case is actually the use of the str2double function. It is known that this built-in function becomes very slow when the data set is large. You might try to get rid of it if possible.
you can use modulo
ans = (36501 - mod(36501,100))/100
This would give you 365
if you want the 1, it is mod(36501,100)
so this would effectively split your second column into 2 different numbers, you can then re name them etc.
hmmm on second thoughts, if all your numbers on your second column are 5 digits, this can be extremely efficient, since mod is computed in matlab by b = a - m.*floor(a./m);
check http://uk.mathworks.com/help/matlab/ref/mod.html it should work for vectors (i.e. your second column)

if greater than matlab

I have a matrix of two columns and six rows, and want to build a second one with the following code:
for i=2
if F(:,i)<50
G(:,i) = 1
end
end
But nothing happens...
The idea was that if a value in the second column in F was less than 50, then the corresponding value in G would be 1.
Sorry for probably basic question, but no idea why this doesn't work. If I change to evaluate whether the F value ~= 50, then everything works as it should.
Thanks for any help.
Your if statement is only executed once - not once per element. While F(:,i)<50 returns an array of values, the if is either true or false; consequently, the next line is only executed once (either on all elements in G(:,i), or none of them).
For example, see this piece of code:
if(1 < [0 1 2]), disp('true'); end
It will produce no output, even though it is true for the third element. On the other hand,
if(1 < [2 3 4]), disp('true'); end
does produce output...
In general, the following:
1 < [0 1 2]
produces
0 0 1
Not sure why you say it doesn't work for < but it does work for ~=. Maybe there are no elements equal to 50, so it only "seems" to work?
In general, there is a better way to do what you want, with a single line:
G(F(:,2)<50,2)=1
This uses "logical indexing", and is much faster than looping. It will consider each element of F(:,2), and modify the corresponding element in G.
One final comment: it is not great practice to use the variable i since it has a built in value of sqrt(-1). If you have code anywhere that relies on it having that value, then accidentally overwriting it with any other value would break that. It's not the problem with your code today - but why set yourself up for a problem in the future.