Why do multiple binary operators in a row work in MATLAB? - matlab

Why is this a valid MATLAB query?
3++4
which evaluates to 7. Even more disturbing:
3+-5
evaluates to -2.
Given the following, I expected
3+*5
to evaluate to 15. Instead it throws an error.
Possible resolution related to thewaywewalk's answer to my previous question at Why is a trailing comma in a cell array valid Matlab syntax?

+ and - are not only binary operators, they are also unary operators.
Documentation:
http://de.mathworks.com/help/matlab/ref/uplus.html
http://de.mathworks.com/help/matlab/ref/uminus.html
For this reasons, the first two lines are evaluated as 3+(+4) and 3+(-5) but the last fails because no unary multiplication exists.

Because Matlab's operator precedence places the unary plus above binary plus.
Therefore,
3++4
is parsed to
plus(3,uplus(4))

Related

Unexpected matlab behaviour when using vectorised assignment

I've come across some unexpected behaviour in matlab that I can't make sense of when performing vectorised assignment:
>> q=4;
>> q(q==[1,3,4,5,7,8])
The logical indices contain a true value outside of the array bounds.
>> q(q==[1,3,4,5,7,8])=1
q =
4 0 1
Why does the command q(q==[1,3,4,5,7,8]) result in an error, but the command q(q==[1,3,4,5,7,8])=1 work? And how does it arrive at 4 0 1 being the output?
The difference between q(i) and q(i)=a is that the former must produce the value of an array element; if i is out of bounds, MATLAB chooses to give an error rather than invent a value (good choice IMO). And the latter must write a value to an array element; if i is out of bounds, MATLAB chooses to extend the array so that it is large enough to be able to write to that location (this has also proven to be a good choice, it is useful and used extensively in code). Numeric arrays are extended by adding zeros.
In your specific case, q==[1,3,4,5,7,8] is the logical array [0,0,1,0,0,0]. This means that you are trying to index i=3. Since q has a single value, reading at index 3 is out of bounds, but we can write there. q is padded to size 3 by adding zeros, and then the value 1 is written to the third element.

setting two values in a matrix at the same time

This is a very basic MATLAB syntax question.
I'm trying to set the first column of a 2x1000 matrix in Matlab to 10 and 30.
This is giving me an error:
xy((1,1),(2,1)) = ([10],
[30])
What am I doing wrong?
Many thanks.
This hardly seems to warrant an answer, but try:
xy(:,1) = [10;30]
Here : means "all elements" in that dimension.
Alternatively you could do either of these:
x([1,2], 1) = [10;30]
x(1:2, 1) = [10;30]
In MATLAB you use round brackets (parentheses) to index into arrays, and use square brackets to define literal arrays. As you can see you can index arrays with arrays, and assign arrays to sub-arrays.

Effective ways to represent logical expression

I am writing a console program to represent logical expressions( something likes AB'C + A'C) so that I can be simplify( optimize) the expressions and evaluate their values. I tried to use string to represent an expression, but in this way, I can only evaluate its value base on input values, but optimize an expression that represented as string is very difficult( with me), Example, ABC + AB can be AB because ABC+AB = AB(C+1) = AB. I have also think another way it is using vector of vector of literal. Example, AB'C + AB + BC will be represent as below figure:
Explaining: Each column is represent for each term, in above example. The first column represents for AB'C, the second one represents for AB and the third one represents for BC'.
I think it is a good way to present logical expression but I still can not find a way to optimize an expression that repression by this way. I also googled but I did not find sample project for the problem.
In short, I hope someone suggest to me a way to represent, evaluate and optimize an logical expression easier. Thank in advance!
How to represent, evaluate and optimize a logical expression?
To represent this you need to use an expression tree and since you are using only logic operators that are binary operators you want to use a binary expression tree or more specifically this.
To simplify the tree you use the laws of Boolean algebra.
If all of the values are bound, then through the process of simplification the tree will simplify to a root node with either true or false.
For some example code I checked Rosetta Code but they had no task for evaluating Boolean expressions. The closest task is arithmetic evaluation.

What is the double-period operator in MATLAB followed by a division operator (../)?

I am analyzing some MATLAB code where there is the following operator: ../. I cannot find any documentation on this operator explaining what it does. Can anyone explain it to me?
sp(it,:) = (ww).*(1../sigt).*exp(-.5*(e(it,:).^2)./(sigt.^2))*srpfrac);
Just being pedantic.
There is no ../ operator, the first . is associated with the 1 indicating a radix point and the ./ is element-wise division. This was likely written by someone who is used to Python where all numbers are considered integers unless the radix point is explicitly included. The more verbose equivalent is:
1.0 ./ sigt
In your case, the 0 has been omitted as it is optional.
To improve readability and future confusion, I would just change it to the following.
1 ./ sigt

Format Matlab data to only have digits after the decimal place

I used dlmwrite to output some data in the following form:
-1.7693255974E+00,-9.7742420654E-04, 2.1528647648E-04,-1.4866241234E+00
What I really want is the following format:
-.1769325597E+00, -.9774242065E-04, .2152864764E-04, -.1486624123E+00
A space is required before each number, followed by a sign, if the number is negative, and the number format is comma delimited, in exponential form to 10 significant digits.
Just in case Matlab is not able to write to this format (-.1769325597E+00), what is it called specifically so that I can research other means of solving my problem?
Although this feels morally wrong, one can use regular expressions to move the decimal point. This is what the function
myFormat = #(x) regexprep(sprintf('%.9e', 10*x), '(\d)\.', '\.$1');
does. The input value is multiplied by 10 prior to formatting, to account for the point being moved. Example: myFormat(-pi^7) returns -.3020293228e+04.
The above works for individual numbers. The following version is also able to format arrays, providing comma separators. The second regexprep removes the trailing comma.
myArrayFormat = #(x) regexprep(regexprep(sprintf('%.9e, ', 10*x), '(\d)\.', '\.$1'), ', $', '');
Example: myArrayFormat(1000*rand(1,5)-500) returned
-.2239749230e+03, .1797026769e+03, .1550980040e+03, -.3373882648e+03, -.3810023184e+03
For individual numbers, myArrayFormat works identically to myFormat.