Label error in Mariesim, Any advice on how to fix it? - marie

I am having an issue in Marie where it is saying that the error is " A label cannot have 0..9 as its beginning character". How would I go about fixing this?
The image below is the section of code where this error appears. I have never seen this before and my friends nor me can find anything on it. Any advice is appreciated!
START, INPUT
SKIPCOND 800
HALT
OUTPUT
STORE A
SUB Z
SKIPCOND 800
SKIPCOND 000
JUMP START
X, INPUT
STORE B
OUTPUT
SUB A
SKIPCOND 800
JUMP Y
JUMP X
Y, LOAD B
ADD C
STORE S
EVEN, SUB T
SKIPCOND 800
JUMP L
JUMP EVEN
L, SKIPCOND 400
JNS ODD
LOAD S
OUTPUT
LOAD A
SUB S
STORE N
OUTPUT
JUMP START
ODD, OUTPUT S
LOAD S
ADD 0
STORE S
JUMPI ODD
A, DEC 00
B, DEC 00
S, DEC 00
N, DEC 04
C, DEC 04
Z, DEC 94
T, DEC 02
0, DEC 01
END START

Related

How to match and copy time-data from one matrix to another?

In MATLAB (R2015a), I have two large matrices that can be simplified as:
A = [ 24 10 830; 24 15 830; 150 17 945; 231 40 1130; 231 45 1130]
(note that in A, column 3 is the time for the event cataloged in column 1) and
B = [24 13; 150 29; 231 43]
How can I match and copy the time-data from column 3 in matrix A, to a matching and filtered event column in matrix B?
For example, I want the value 24 from first column in B to be matched with value 24 from first column in A, and then copy the corresponding time-data in A's third column (for 24 it's 830, for 150 it's 945 etc.) into B. This should result into our new B with the time-data from A:
B = [24 13 830; 150 29 945; 231 43 1130]
I relatively new to MATLAB so any help is much appreciated!
First find the location of the elements in the first row of B in the first row of A using the ismember function. And then use those locations to construct the new matrix.
[~,Locb] = ismember(B(:,1),A(:,1));
Bnew = [B A(Locb,3)]
Bnew =
24 13 830
150 29 945
231 43 1130
This is a fast way that comes to my mind. There might be some singularities that needed to be checked more thoroughly.

How to use the 'if' statement in matlab?

I have a cell array of size 5x5 as below
B= 00 10 11 10 11
01 01 01 01 11
10 00 01 00 01
10 10 01 01 11
10 10 10 00 10
And two column vectors
S1= 21
23
28
25
43
S2= 96
85
78
65
76
I want to create a new cell array of the same size as B say 5x5 such that it satisfies the following condition
Final={S1 if B{i}=11
S1 if B{i}=10
S2 if B{i}=01
S2 if B{i}=00
So the resulting output would be something like this
Z = s2 s1 s1 s1 s1
s2 s2 s2 s2 s1
s1 s2 s2 s2 s2
s1 s1 s2 s2 s1
s1 s1 s1 s2 s1
ie Z= 96 21 21 21 21
85 85 85 85 23
28 78 78 78 78
25 25 65 65 25
43 43 43 76 43
I tried using the if condition but i get error saying
'Error: The expression to the left of the equals sign is not a valid target for an assignment.'
for i=1:1:128
for j=1:1:16
if fs{i,j}=00
Z{i,j}=S1{i,j}
elseif fs{i,j}= 01
Z{i,j}=S2{i,j}
elseif fs{i,j}= 10
Z{i,j}=S1{i,j}
elseif fs{i,j}= 11
Z{i,j}=S2{i,j}
end
end
I think I'm making a mistake in the if statement as well as the expressions I'm using. Where am i going wrong? Please help thanks in advance.
Use == for comparison and = for assignment. So if fs{i,j}==00, etc.
Edit: Matlab is really designed for highly vectorized operations. Nested loops are slow compared to native functions, and typically can be replaced with vectorized versions. Is there any particular reason why you are using cell arrays instead of matrices, especially when you only have numeric data?
If B, S1, and S2 were matrices your code could be written in one highly efficient line that will run much much faster:
Z = bsxfun(#times, S1, B == 11 | B == 10) + bsxfun(#times, S2, B == 01 | B == 0)
Since B is a cell array you will want to convert it to a matrix using cell2mat unless you'd like to use cellfun.
Instead, you can just call B_mat = cell2mat(B), followed by (B_mat>=10).*repmat(S1,1,5) + (B_mat<10).*repmat(S2,1,5).
It's possible that your cell array actually contains binary values, possibly represented as strings, in which case the conditions used above would need to be changed. Then using cellfun may be necessary.

how to store 00 as 00 instead of 0 only

MATLAB stores 00 and 01 as 0 and 1 respectively. How can I make MATLAB store 00 as 00 and 01 as 01 instead of 0 and 1 only...here is my code.. I am talking about the statements with <-- only..In fact I want to input the result as initial population(chromosome) to a genetic algorithm.
function [x]=abc()
r=randi([0 3],1,20);
for i=1:20
if r(i)==0
x(i)=00; %// <--
elseif r(i)==1
x(i)=01; %// <--
elseif r(i)==2
x(i)=10;
elseif r(i)==3
ex(i)=11;
end
end
end
It looks like you want to store the binary representation of your numbers, so you can use the function dec2bin
and the best thing, you don't even need a loop ;)
r=randi([0 3],1,20);
x = dec2bin(r,2) ;
>> x
x =
10
00
11
11
10
11
10
01
...

Matlab vectorisation of loop "tracking value increments"

I have been breaking my head about trying to optimise a matlab script that needs to process quite long arrays.
Basically, there are 2 arrays: AbsoluteTimeTag and Channel.
AbsoluteTimeTag will hold int values signifying time that are recorded using a 16 bit counter. Since the counter is limited to 2^16 values it will often roll over during the course of the measurement. Channel registers this and when it does, bitand(Channel,2048) will evaluate true.
How these arrays are generated is out of my control and the overflow markers occur "in sync" with the data coming in.
It is now easy to reconstruct the absolute time by doing:
AbsoluteTimeTag(i) = NoOfOverflows * 65536 + TimeTag(i);
In a loop:
for i=1:NumberOfRecords
%Running through all records, if we meet anything other than an
%overflow record, we van just calculate its absolute time based on
%current overflow count and its timetag.
AbsoluteTimeTag(i) = NoOfOverflows * 65536 + TimeTag(i);
% We are running through all records so if we encounter an overflow
% (signified by bitand(..., 2048), we add 1 to the overflow count.
if bitand(Channel(i),2048)
NoOfOverflows = NoOfOverflows + 1;
end;
end;
I have been really breaking my head on how to potentially vectorise this (since Matlab is pretty bad at loops). So far however, I do not see the light for some reason.
Problem is that the number of overflows up to a certain record index along AbsoluteTimeTag can change as you walk the vector from start to end.
I do not know how I could express "count all occurrences of overflow up to this point" in a vectored operation.
Can somebody comment on whether or not this might be feasible at all?
EDIT
Example data would look like this:
TimeTag (each element is an event for which time is counted un a 2^3 register for simplicity):
[ 01 03 04 07 xx 02 03 04 05 xx 01 03 04 07 xx ... ]
overflow:
[ 00 00 00 00 01 00 00 00 00 01 00 00 00 00 01 ... ]
Which would need to yield
[ 01 03 04 07 xx 10 11 12 13 xx 17 19 20 23 xx ... ]
The reason xx is there is because originally, all records, both the events and the overflows are in one large uint32 array where each record holds different types of info on the different bits. The xx locations can undergo any operation but they are further meaningless when considering the TimeTag records. I have a logical array keeping track of what locations hold actual meaningful data.
"count all occurrences of ... to this point" in a vectored operation would be best expressed as cumsum.
Assuming NoOfOverflows is initialized as zero before the start of the loop and AbsoluteTimeTag, Timetag have NumberOfRecords as their number of elements , see if this works for you -
%// Get the overflow for all Channel entries in one go
bitand_all = bitand(Channel,2048)~=0;
%// AbsoluteTimeTag appears to be a cumsum of overflows plus Timetag
AbsoluteTimeTag = [0 ; cumsum(bitand_all(1:end-1).*65536)]+ TimeTag;
%// Get the count of overflows as NoOfOverflows
NoOfOverflows = sum(bitand_all);

Interpolate the time stamped patient data

Question: I am dealing with a patient data, where parameters are recorded at different sampling frequency, so having a different time stamp.
I want to create a matrix where data is interpolated by "Last known value" till the new original value changes in the time. So at the end I have uniform matrix where each parameter have values at every time stamp.
Data is in following format:
Time Hear Rate(Variable)
18:00:00 PM 74
18:02:00 PM 75
18:04:00 PM 85
18:06:00 PM 71
18:08:00 PM 79
18:10:00 PM 72
Time Blood Press. (Variable)
18:01:00 PM 100
18:05:00 PM 120
18:09:00 PM 121
Target :
Time Hear Rate(Variable) Blood Press.
18:00:00 PM 74 NaN
18:01:00 PM 74 100
18:02:00 PM 75 100
18:03:00 PM 75 100
18:04:00 PM 85 100
18:05:00 PM 85 120
18:06:00 PM 71 120
18:07:00 PM 71 120
18:08:00 PM 79 120
18:09:00 PM 79 121
18:10:00 PM 72 121
The Interpolated data in the missing place should be the previous value of the known event and should remain same until next change occurs.
I am currently referring the following thread from MATLAB User Forum
http://www.mathworks.com/matlabcentral/answers/101237-how-can-i-do-1-d-interpolation-with-interp1-to-find-the-nearest-value-to-the-left-of-the-point-i-e
You're essentially trying to do a zero-order hold as you've seen from the link you provided.
Let's call the first set of times t_hr (a cell array of strings) and the second set t_bp (also a cell array of strings). Then call the heart rates hr and the blood pressures bp.
Build a new cell array t by combining t_hr and t_bp:
t = [t_hr; t_bp];
Fill in hr and bp with some NaNs to make them the same length as t. In t, the first part of the vector corresponds to times we know about for hr, and the second half corresponds to bp. Use that knowledge accordingly:
hr = [hr; nan(length(t) - length(hr),1)];
bp = [nan(length(t) - length(bp),1); bp];
Now you've got three vectors: t (okay, that's actually a cell array); hr and bp, and the elements of hr and bp correspond to elements in t, including the NaNs:
t =
'18:00:00 PM'
'18:02:00 PM'
'18:04:00 PM'
'18:06:00 PM'
'18:08:00 PM'
'18:10:00 PM'
'18:01:00 PM'
'18:05:00 PM'
'18:09:00 PM'
hr =
74
75
85
71
79
72
NaN
NaN
NaN
bp =
NaN
NaN
NaN
NaN
NaN
NaN
100
120
121
Now, we can sort t:
[t_sorted, idx] = sort(t);
idx contains the indices of t that got moved around to form t_sorted. In this case, idx == [1 7 2 3 8 4 5 9 6].'. We can use that to sort bp and hr:
hr_sorted = hr(idx)
hr_sorted =
74
NaN
75
85
NaN
71
79
NaN
72
bp_sorted = bp(idx)
bp_sorted =
NaN
100
NaN
NaN
120
NaN
NaN
121
NaN
Then, apply the zero-order hold:
for ii = 2:length(t)
if isnan(hr_sorted(ii))
hr_sorted(ii) = hr_sorted(ii-1);
end
if isnan(bp_sorted(ii))
bp_sorted(ii) = bp_sorted(ii-1);
end
end
and your final vectors become:
hr_sorted =
74
74
75
85
85
71
79
79
72
bp_sorted =
NaN
100
100
100
120
120
120
121
121
Note that your target answer has 11 different times in it, but you only supplied nine. (It's missing 18:03 and 18:07.) You can easily extend this answer by adding extra NaNs to hr and bp, and making t a cell array comprising of, say, t_hr, t_bp, and a cell array t_missing ={'18:03:00 PM', '18:07:00 PM'}.