Most efficient way to find all unique solutions of q
1 view (last 30 days)
Show older comments
I am designing a convex program on MATLAB whose if-else functionality is being modelled with a binary vector q that is 1xN vector. The solution of each element of the vector q is based on another variable t_ref, that is just a vector that represents a uniform step size from a certain t (current time) to a t_f (final time).
t_ref=t:(t_f-t)/(N-1):tf
t_ref is also therefore a 1xN vector.
q is then calculated by the following:
q=(t_ref<=t1) | (t_ref>=t2);
Meaning that for every value of t that is greater than or equal to a certain constant value t2 and , or less than or equal to t1 (both t1 and t2 are between 0 and tf) , q=1, and 0 every where else.
t can take on a value from anywhere between 0 and tf. The only solution I have been able to come up with is running a line search with the input being t and a small enough step size between different t values (between t0 and tf always) and then obtain the matrix of unique solutions of vector q.
I feel like there may be a more efficient trick to solving this? Or am I at the mercy of a P vs. NP problem?
0 Comments
Answers (1)
arushi
on 5 Oct 2023
Hi Alessandro,
I understand that you would like to efficiently calculate the binary vector ‘q’ based on the vector ‘t_ref’ and two constant values ‘t1’ and ‘t2’. The value of ‘q’ should be 1 when ‘t_ref’ is greater than or equal to ‘t2’ or less than or equal to ‘t1’, and 0 otherwise. Instead of using a line search, you can directly calculate the binary vector ‘q’ using logical operations without iterating through each element of ‘t_ref’. Here's an efficient approach:
q = (t_ref <= t1)| (t_ref >= t2);
In this approach, the logical operator ‘<=’ compares each element of ‘t_ref’ with ‘t1’, resulting in a logical vector of the same size as ‘t_ref’ with 1 where the condition is true and 0 otherwise. Similarly, the logical operator ‘>=’ compares each element of ‘t_ref’ with ‘t2’. Finally, the logical OR operator ‘|’ combines the two conditions elementwise, resulting in the desired binary vector ‘q’ .
This approach avoids the need for a line search or iterating through each element of ‘t_ref’, making it more efficient.
Note that ‘t1’ and ‘t2’ should be scalar values, not vectors, as per your description. If you have multiple values of ‘t1’ and ‘t2’, you can use element-wise comparisons and logical operations to calculate ‘q’ for each pair of ‘t1’ and ‘t2’ values.
Hope this answers your question.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!