Receiving invalid left hand side of assignment error where comparison is not appropriate - octave

for [1:length(Vector)]
Unique = unique(Vector)
if any(ismember(Vector(i),Unique))
v=Vector(i)
Vector(i)=[]
Unique = Unique(~ismember(v,Unique))
endif
endfor
Here I am writing code, in octave, to remove all elements of Vector that aren't duplicates, leaving one less copy of each duplicate element in Vector. I am receiving error in line 2, where Unique is defined. The error is 'invalid left hand side of assignment error' which, after googling, is commonly due to use of assignment instead of comparison. However, comparison is not appropriate here.
Please can someone help me.

Related

How to use RANSAC method to fit a line in Matlab

I am using RANSAC to fit a line to my data. The data is 30X2 double, I have used MatLab example to write the code given below, but I am getting an error in my problem. I don't understand the error and unable to resolve it.
The link to Matlab example is
https://se.mathworks.com/help/vision/ref/ransac.html
load linedata
data = [xm,ym];
N = length(xm); % number of data points
sampleSize = 2; % number of points to sample per trial
maxDistance = 2; % max allowable distance for inliers
fitLineFcn = polyfit(xm,ym,1); % fit function using polyfit
evalLineFcn =#(model) sum(ym - polyval(fitLineFcn, xm).^2,2); % distance evaluation function
[modelRANSAC, inlierIdx] = ransac(data,fitLineFcn,evalLineFcn,sampleSize,maxDistance);
The error is as follows
Error using ransac Expected fitFun to be one of these types:
function_handle
Instead its type was double.
Error in ransac>parseInputs (line 202) validateattributes(fitFun,
{'function_handle'}, {'scalar'}, mfilename, 'fitFun');
Error in ransac (line 148) [params, funcs] = parseInputs(data, fitFun,
distFun, sampleSize, ...
Lets read the error message and the documentation, as they tend to have all the information required to solve the issue!
Error using ransac Expected fitFun to be one of these types:
function_handle
Instead its type was double.
Hum, interesting. If you read the docs (which is always the first thing you should do) you see that fitFun is the second input. The error says its double, but it should be function_handle. This is easy to verify, indeed firLineFun is double!
But why? Well, lets read more documentation, right? polyfit says that it returns an array of the coefficient values, not a function_hanlde, so indeed everything the documentation says and the error says is clear about why you get the error.
Now, what do you want to do? It seems that you want to use polyfit as the function to fit with ransac. So we need to make it a function. According to the docs, fitFun has to be of the form fitFun(data), so we just do that, create a function_handle for this;
fitLineFcn=#(data)polyfit(data(:,1),data(:,2),1);
And magic! It works!
Lesson to learn: read the error text you provide, and the documentation1, all the information is there. In fact, I have never used ransac, its just reading the docs that led me to this answer.
1- In fact, programmers tend to reply with the now practically a meme: RTFM often, as it is always the first step on everything programming.

What can be used instead of Either in DAML?

when am creating choice using either function,whenever error has been occurs means then they returns values to the left side of the either,which is terminating the complete template itself that is not proceeding further for other scenario execution,how to do both same functionality in daml instead of Either.
If execution doesn't proceed further, then the error is not handled. Moreover, a Left can't be simply ignored. Consider this DAML function:
steps : Bool -> Either Int Bool
steps q = do
a <- if q then Left 1 else Right "foobar"
return $ a == "foobar"
a is a Text, which is only present if the Either is Right. So if the Either is Left, execution cannot proceed to the last line, because there is nothing to assign to a.
It wouldn't do to change this behavior just because you might get an Either Text Text. So in this case, too, the variable will only be bound if it's Right.
It also wouldn't do to change behavior just because you removed the variable. For example,
steps2 : Bool -> Either Int Bool
steps2 q = do
if q then Left 1 else Right "foobar"
return q
If the semantics suddenly "just kept going" because you eliminated an unused variable binding, that would be incredibly inconsistent and confusing. So it stops right there on Left, just as if a <- was still there.
The thing is, this is not just about Either; this holds for all "error-reporting-style" actions, because they are all characterized by "I don't have a value to bind to your variable", so execution can never proceed further in any do, even if you use an "alternative" to Either.
You must handle the error right there if you want execution to proceed; in other words, if you have a Left, you have to come up with a Right if you want it to keep going, and that has equivalence in any action that features error-reporting, because they all have missing a values that you must come up with. Indeed, the meaning of error for all is "I can't come up with a value for your a <- or whatever else you're doing".

Erlang - ** exception error: no match of right hand side value

I have a problem with pattern match of json formatted string.
Here I add a shorted version (just changed long json string to "{\"jsondata\"}"
So i have this pattern match which is sucessfull:
> MyData2={ok,{{"HTTP/1.1",200,"OK"},
[{"connection","Keep-Alive"},
{"date","Thu, 10 Sep 2015 12:03:49 GMT"},
{"server","Apache/2.4.7 (Ubuntu)"},
{"vary","X-Auth-Token"},
{"content-length","1171"},
{"content-type","application/json"},
{"x-openstack-request-id",
"req-31b4efc1-2af4-4130-b7a8-01d94b456096"},
{"keep-alive","timeout=5, max=100"}],
"{\"jsondata\"}"}}.
After that I run the following:
> {ok,{{"HTTP/1.1",ReturnCode, State},B,J}}=MyData2.
unfortunatelly i get
If I change "{\"jsondata\"}" to "jsondata" the last pattern match works fine
I have no Idea how to extract the json and get in J the "{\"jsondata\"}"
I`ll appriciate any idea
** exception error: no match of right hand side value
Your pattern matching operation works perfectly. I think the problem is, that one of the variables ReturnCode, State, B or J is already bound.
Lets assume the variable J is already bound to a value, and the other variables are not. Depending on this value, the pattern matching operation
{ok,{{"HTTP/1.1",ReturnCode, State},B,J}} = MyData2.
either succeeds or not.
Case 1:J is already bound to "{\"jsondata\"}"
Your pattern-match will succeed and the values of the unbound variables (ReturnCode, State and B) will be set, according to the pattern of MyData2.
Case 2:J is already bound to "{jsondata}"
The J-variable on the right hand side won't match the pattern of MyData2 on the left hand side. Thus the execution fails with an exception.
This also happens on the shell if you forget to clear your variables with f(Variable).

Can I read the rest of the line after a positive value of IOSTAT?

I have a file with 13 columns and 41 lines consisting of the coefficients for the Joback Method for 41 different groups. Some of the values are non-existing, though, and the table lists them as "X". I saved the table as a .csv and in my code read the file to an array. An excerpt of two lines from the .csv (the second one contains non-exisiting coefficients) looks like this:
48.84,11.74,0.0169,0.0074,9.0,123.34,163.16,453.0,1124.0,-31.1,0.227,-0.00032,0.000000146
X,74.6,0.0255,-0.0099,X,23.61,X,797.0,X,X,X,X,X
What I've tried doing was to read and define an array to hold each IOSTAT value so I can know if an "X" was read (that is, IOSTAT would be positive):
DO I = 1, 41
(READ(25,*,IOSTAT=ReadStatus(I,J)) JobackCoeff, J = 1, 13)
END DO
The problem, I've found, is that if the first value of the line to be read is "X", producing a positive value of ReadStatus, then the rest of the values of those line are not read correctly.
My intent was to use the ReadStatus array to produce an error message if JobackCoeff(I,J) caused a read error, therefore pinpointing the "X"s.
Can I force the program to keep reading a line after there is a reading error? Or is there a better way of doing this?
As soon as an error occurs during the input execution then processing of the input list terminates. Further, all variables specified in the input list become undefined. The short answer to your first question is: no, there is no way to keep reading a line after a reading error.
We come, then, to the usual answer when more complicated input processing is required: read the line into a character variable and process that. I won't write complete code for you (mostly because it isn't clear exactly what is required), but when you have a character variable you may find the index intrinsic useful. With this you can locate Xs (with repeated calls on substrings to find all of them on a line).
Alternatively, if you provide an explicit format (rather than relying on list-directed (fmt=*) input) you may be able to do something with non-advancing input (advance='no' in the read statement). However, as soon as an error condition comes about then the position of the file becomes indeterminate: you'll also have to handle this. It's probably much simpler to process the line-as-a-character-variable.
An outline of the concept (without declarations, robustness) is given below.
read(iunit, '(A)') line
idx = 1
do i=1, 13
read(line(idx:), *, iostat=iostat) x(i)
if (iostat.gt.0) then
print '("Column ",I0," has an X")', i
x(i) = -HUGE(0.) ! Recall x(i) was left undefined
end if
idx = idx + INDEX(line(idx:), ',')
end do
An alternative, long used by many many Fortran programmers, and programmers in other languages, would be to use an editor of some sort (I like sed) and modify the file by changing all the Xs to NANs. Your compiler has to provide support for IEEE NaNs for this to work (most of the current crop in widespread use do) and they will correctly interpret NAN in the input file to a real number with value NaN.
This approach has the benefit, compared with the already accepted (and perfectly good) answer, of not requiring clever programming in Fortran to parse input lines containing mixed entries. Use an editor for string processing, use Fortran for reading numbers.

What does Backpatching mean?

What does backpatching mean ? Please illustrate with a simple example.
Back patching usually refers to the process of resolving forward branches that have been planted in the code, e.g. at 'if' statements, when the value of the target becomes known, e.g. when the closing brace or matching 'else' is encountered.
In intermediate code generation stage of a compiler we often need to execute "jump" instructions to places in the code that don't exist yet. To deal with this type of cases a target label is inserted for that instruction.
A marker nonterminal in the production rule causes the semantic action to pick up.
Some statements like conditional statements, while, etc. will be represented as a bunch of "if" and "goto" syntax while generating the intermediate code.
The problem is that, These "goto" instructions, do not have a valid reference at the beginning(when the compiler starts reading the source code line by line - A.K.A 1st pass). But, after reading the whole source code for the first time, the labels and references these "goto"s are pointing to, are determined.
The problem is that can we make the compiler able to fill the X in the "goto X" statements in one single pass or not?
The answer is yes.
If we don't use backpatching, this can be achieved by a 2 pass analysis on the source code. But, backpatching lets us to create and hold a separate list which is exclusively designed for "goto" statements. Since it is done in only one pass, the first pass will not fill the X in the "goto X" statements because the comipler doesn't know where the X is at first glance. But, it does stores the X in that exclusive list and after going through the whole code and finding that X, the X is replaced by that address or reference.
Backpaching is the process of leaving blank entries for the goto instruction where the target address is unkonown in the forward transfer in the first pass and filling these unknown in the second pass.
Backpatching:
The syntax directed definition can be implemented in two or more passes (we have both synthesized attributes and inherited attributes).
Build the tree first.
Walk the tree in the depth-first order.
The main difficulty with code generation in one pass is that we may not know the target of a branch when we generate code for flow of control statements
Backpatching is the technique to get around this problem.
Generate branch instructions with empty targets
When the target is known, fill in the label of the branch instructions (backpatching).
backpatching is a process in which the operand field of an instruction containing a forward reference is left blank initially. the address of the forward reference symbol is put into this field when its definition is encountered in the program.
Back patching is the activity of filling up the unspecified information of labels
by using the appropriate semantic expression in during the code generation process.
It is done by:
boolean expression.
flow of control statement.