matlab search a matching element - matlab

I have an integer array of length 2000 elements. For ex
x = [2, 4, 5, 6, 5,6,7,5......];
Now in this array i need to find an element which occurs repeatedly. For ex I need to know how many times a number '5' has been occurred. In the above example it is three times.
Is there any way to search an matching element and returns the count in matlab?

Do you know the number in advance?
If so, to work out how many times it appears in x you could do:
sum(x==5)
The x==5 creates a vector of [FALSE FALSE TRUE FALSE TRUE FALSE FALSE TRUE ...], being TRUE whenever x is 5.
The sum then adds up that vector, where FALSE maps to 0 and TRUE to 1.

A quick way get the count is
sum(x == 5)
If you need the indicies of the matching elements:
find(x == 5)
Notice, the count is also length(find(x == 5)).
Standard caveats apply toward the use of == and floating point numbers.

Related

Check of non-overlapping elements postgres

Postgres documentation seems to only show how to check if two arrays have overlapping elements.
I need to know if the two array have non-overlapping elements.
So if I have 2 arrays:
array_1 = '{1, 2, 3, 4}'
array_2 = '{0, 3, 4, 5}'
This should return false. Checking for not equal doesn't work because they may not be equal in that one array might have the same integer repeat several times.
Is this comparison possible?
Two sets are non-overlapping if there are no elements in common, so using the && operator for arrays and negating the result give you what you want.
# select NOT (ARRAY[1,2,3,4] && ARRAY[0,3,3,3,3,4,5]) AS non_overlapping;
non_overlapping
-----------------
f
(1 row)
# select NOT (ARRAY[1,2,8,9] && ARRAY[0,3,3,3,3,4,5]) AS non_overlapping;
non_overlapping
-----------------
t
(1 row)

Check if assigned elements satisfy a condition in OR-Tools

I have say 100 elements that I want to assign to say 10 spots.
# the elements list holds 100 variables that signify the assignment to a spot
elements = [model.NewIntVar(1, 10) for i in range(100)]
Each of my element has a specific size. Now I want to model one (set of) constraint(s) per spot that says: The added sizes of all elements assigned to this spot lies in a fixed range.
So if spot 1 gets elements 1, 16 and 64 assigned, and their sizes are 1521, 1732, 1431 and my range is (3000, 6000) that would be ok. But if too many or too large elements (or too few/small) get assigned to spot 1, that would not be ok.
Something like the following, which does not work:
for spot in range(10):
sum_ = sum([get_size(e) for e in elements if e == spot]) # if think if e == spot is what fails
model.Add(sum_ >= 3000)
model.Add(sum_ <= 6000)
How can I model such a thing? I have looked at channeling constraints but I can't quite wrap my head around it.
I think it is better to model the assignment as a boolean:
from ortools.sat.python import cp_model
model = cp_model.CpModel()
solver = cp_model.CpSolver()
all_spots = range(10)
all_elems = range(100)
elements = {
(elem, spot): model.NewBoolVar(f"{elem} in spot {spot}")
for elem in all_elems
for spot in all_spots
}
# only one spot for element
for elem in all_elems:
model.Add(sum(elements[elem, spot] for spot in all_spots) == 1)
for spot in all_spots:
# taking the element id as its size
sum_ = sum(elements[elem, spot] * elem for elem in all_elems)
model.Add(sum_ >= 0)
model.Add(sum_ <= 500)
solver.Solve(model)
for (elem, spot), boolean in elements.items():
if solver.Value(boolean):
print(boolean)
See:
https://github.com/google/or-tools/blob/stable/ortools/sat/samples/multiple_knapsack_sat.py
https://github.com/google/or-tools/blob/stable/ortools/sat/samples/binpacking_problem_sat.py
https://github.com/google/or-tools/blob/stable/examples/python/balance_group_sat.py#L102

Trying to loop over index values - Intend to to get False value for every index number divided by 2

I have a list such as:
bool_list = [False,False,True,True,True,True]
Now I plan to run a for loop to make sure that every index value divided by 2 gets converted to False. In case, the index value is not divided by 2 then it remains the same.
For example, I want a result in which the bool_list will display =
[False,False,False,True,False,True] as the 2nd and 4th index has been turned to False.
I have tried to write the following for loop using the enumerate function to get the index value but somehow it's giving me an error:
def mark_false(bool_list, p):
for idx,val in enumerate(bool_list):
if idx % p ==0:
bool_list[idx] ==False
else:
bool_list[idx] = bool_list[idx]
return (bool_list)
The function is going to be with p = 2
You can do this without a loop by replacing the slice of every other index with a list of False objects of the same length:
>>> bool_list = [False,False,True,True,True,True]
>>> bool_list[::2] = [False] * len(bool_list[::2])
>>> bool_list
[False, False, False, True, False, True]
I assume this is what your are looking for.
just changes 4th and 6th element of bool_list.
def mark_false(bool_list, p):
for i in range(2 * p, len(bool_list), p):
bool_list[i] = False
return bool_list
print(mark_false(list_true(6), 2))

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]

How to take one particular number or a range of particular number from a set of number?

I am looking for to take one particular number or range of numbers from a set of number?
Example
A = [-10,-2,-3,-8, 0 ,1, 2, 3, 4 ,5,7, 8, 9, 10, -100];
How can I just take number 5 from the set of above number and
How can I take a range of number for example from -3 to 4 from A.
Please help.
Thanks
I don't know what you are trying to accomplish by this. But you could check each entry of the set and test it it's in the specified range of numbers. The test for a single number could be accomplished by testing each number explicitly or as a special case of range check where the lower and the upper bound are the same number.
looping and testing, no matter what the programming language is, although most programming languages have builtin methods for accomplishing this type of task (so you may want to specify what language are you supposed to use for your homework):
procfun get_element:
index=0
for element in set:
if element is 5 then return (element,index)
increment index
your "5" is in element and at set[index]
getting a range:
procfun getrange:
subset = []
index = 0
for element in set:
if element is -3:
push element in subset
while index < length(set)-1:
push set[index] in subset
if set[index] is 4:
return subset
increment index
#if we met "-3" but we didn't met "4" then there's no such range
return None
#keep searching for a "-3"
increment index
return None
if ran against A, subset would be [-3,-8, 0 ,1, 2, 3, 4]; this is a "first matched, first grabbed" poorman's algorithm. on sorted sets the algorithms can get smarter and faster.