How to assign -INFINITY amongst others to multiple variables? - matlab

I have this line of pseudocode that I am trying to translate in Matlab:
(maxSum, maxStartIndex, maxEndIndex) := (-INFINITY, 0, 0)
I have translated the second and third variables simply assigning a 0:
maxStartIndex=0;
maxEndIndex=0;
How should I translate this line?
maxSum= -INFINITY
I have not find reference for this.

Probably you're just looking for deal:
[maxSum, maxStartIndex, maxEndIndex] = deal(-inf,0,0)

Related

map and sort from alphanumeric data

I have the file "global power plants" with a column "capacity_in_mw" (with numbers 30, 100, 45, ...) and another column is "primary_fuel" (Coal, Hydro, Oil, Solar, Nuclear, Wind, Coal).
I can generate a map in function of "capacity_in_mw" by setting the condition
plotdata = data.query('capacity_in_mw > 50')
Now, I would like to generate a map in function of "primary_fuel". Because data is alphanumeric, how do I set up the condition?
Furthermore, when making the map, to assign color='black' for Coal, color='green' for Wind, color='yellow' for Solar, ... etc.
Python.
I am a novice, but I think I found the solution. It is more of an issue of syntax, to use the == in the query to identify the alphanumeric value.
plotdata = data.query('primary_fuel == "Hydro"')
Also, lesson learned in the future to dig more before posting a question.

How to make a for loop to go through two arrays in MATLAB

I want to make a for loop that goes from 0 to 180, and then back again to -180. I tried the following:
for a=0:1:180 && 179:-1:-180
but this is not possible in MATLAB.
I have tried to use the && and || statements, but both don't work. I don't know any other ways to combine the two arrays. Any ideas?
You misunderstand the && and || operators. What you want is the following:
Go from 0 to 180 in steps of 1 AND then go from 180 to -180 in steps of -1.
However for any two statements A and B (both A and B need to be scalar values!), the command A && B does the following:
Return True, if both A and B are True, return False otherwise.
This is a logical AND, while you want to go through your first array AND through your second array after that. Though both is some kind of AND, you can't use && for your purpose.
Now, when you call for a=0:180, MATLAB does the following:
Create the vector 0:180, that is [0, 1, 2, ..., 180].
Run all the content inside the loop for each element in the vector created in 1).
So, what you want to do is create an array that contains the numbers [0, 1, 2, ..., 179, 180, 179, 178, ..., -179, -180]. You can do that by concatenating the arrays [0:180] and [179:-1:-180]. You should read about concatenation in MATLAB in their documentation. So, long story short, you for loop should be
for a=[0:180, 179:-1:-180]

z3py: How to implement a counter in z3?

I want to design logics similar to a counter in Z3py.
If writing python script, we usually define a variable "counter" and then keep incrementing it when necessary. However, in Z3, there is no variant. Therefore, instead of defining an variant, I define a trace of that variant.
This is a sample code. Suppose there is an array "myArray" of size 5, and the elements in the array are 1 or 2. I want to assert a constraint that there must be two '2's in "myArray"
from z3 import *
s = Solver()
myArray = IntVector('myArray',5)
for i in range(5):
s.add(Or(myArray[i]==1,myArray[i]==2))
counterTrace = IntVector('counterTrace',6)
s.add(counterTrace[0]==0)
for i in range(5):
s.add(If(myArray[i]==2,counterTrace[i+1]==counterTrace[i]+1,counterTrace[i+1]==counterTrace[i]))
s.add(counterTrace[5]==2)
print s.check()
print s.model()
My question is that is this an efficient way of implementing the concept of counter in Z3? In my real problem, which is more complicated, this is really inefficient.
You can do this but it is much easier to create the sum over myArray[i] == 2 ? 1 : 0. That way you don't need to assert anything and you are dealing with normal expressions.

Find value in vector "p" that corresponds to maximum value in vector "r = f(p)"

As simple as in title. I have nx1 sized vector p. I'm interested in the maximum value of r = p/foo - floor(p/foo), with foo being a scalar, so I just call:
max_value = max(p/foo-floor(p/foo))
How can I get which value of p gave out max_value?
I thought about calling:
[max_value, max_index] = max(p/foo-floor(p/foo))
but soon I realised that max_index is pretty useless. I'm sorry asking this, real beginner here.
Having dropped the issue to pieces, I realized there's no unique corrispondence between values p and values in my related vector p/foo-floor(p/foo), so there's a logical issue rather than a language one.
However, given my input data, I know that the solution is unique. How can I fix this?
I ended up doing:
result = p(p/foo-floor(p/foo) == max(p/foo-floor(p/foo)))
Looks terrible, so if you know any other way...
Once you have the index, use it:
result = p(max_index)
You can create a new vector with your lets say "transformed" values:
p2 = (p/foo-floor(p/foo))
and then just use find to find the max values on p2:
max_index = find(p2 == max(p2))
that will return the index or indices of p2 with the max value of that operation, and finally just lookup the original value in p
p(max_index)
in 1 line, this is:
p(find((p/foo-floor(p/foo) == max((p/foo-floor(p/foo))))))
which is basically the same thing you did in the end :)

Is there a better way to write this in Matlab?

I have this code, but there must be a better efficient to write it:
rt= RealTrans;
rtsize=size(rt);
rtrows=rtsize(1);
Relative_Axis_Moves=[rt(1,1) rt(1,2) rt(1,3) rt(1,4) rt(1,5);
rt(2:rtrows,1)-rt(1:rtrows-1,1) rt(2:rtrows,2)-rt(1:rtrows-1,2)
rt(2:rtrows,3)-rt(1:rtrows-1,3) rt(2:rtrows,4)-rt(1:rtrows-1,4)
rt(2:rtrows,5)-rt(1:rtrows-1,5)];
There are two rows in the matrix. The first row ends at rt(1,5).
I also have the following code:
p1size=size(p1);
p1rows=p1size(1);
flank_edge_point=[0 0 0; p1(2:p1rows,2)-p1(1:p1rows-1,2) xy(2:p1rows,1)-xy(1:p1rows-1,1) xy(2:p1rows,2)-xy(1:p1rows-1,2); 0 0 xy(p1rows,2)];
How do i get xy(p1rows,2) value in matlab without p1rows?
I also have the code below which relies on the number of rows:
RAMrow=size(Relative_Axis_Moves);
RAMrow=RAMrow(1);
for i=1:RAMrow
L(i)= norm(Relative_Axis_Moves(i,:));
end
L=L';
L(RAMrow+1)= 0;
Any way to write this code more succinctly and efficiently would be greatly appreciated.
Most likely, there will be more than two rows in Relative_Axis_Moves, since the differences in the second row evaluate to arrays.
Anyway, a compact way of writing this is
Relative_Axis_Moves = [RealTrans(1,1:5);diff(RealTrans(:,1:5),1,1)];