manipulating divisible numbers? - numbers

if you have an integer array n and you want to check if n[i] divisible by any of these numbers for example: 2,3,5.
all possible results:
1) n[i] is divisible by 2 only
2) n[i] is divisible by 3 only
3) n[i] is divisible by 5 only
4) n[i] is divisible by 2 and 3
5) n[i] is divisible by 3 and 5
6) n[i] is divisible by 2 and 5
7) n[i] is divisible by 2, 3 and 5
if you decide only to know the first 3 results, is there a method to derive the rest of results from it ?
because if the numbers to check the divisibility against them where many numbers the possible results will be exponentially many results too.
for example i have x numbers from the array and i know how many of them are divisible by 2 and how many are divisible by 3 and how many are divisible by 5, but i want to know for example how many are divisible by 2 or 3 (union), if i added the count of numbers divisible by 2 to the count of numbers divisible by 3 many numbers will be duplicated (the intersection which have a set divisible by both 2 and 3).
so is there a way to know this intersection from the only information that i have (the information: count of numbers divisible by 2, and count of numbers divisible by 3, and count of numbers divisible by 5) ?

No. For example, [2,3,4,9,5] and [2,3,6,5,7] both have the same number of entries divisible by 2,3, and 5, but differ as to the number of entries divisible by 6.
On edit. Here is an even simpler example: [2, 5, 6] vs. [2, 3, 10]. Both of these have 2 even numbers, 1 multiple of 3, and 1 multiple of 5, but they differ as to the number of elements divisible by 6 = 2*3 and the number of elements divisible by 10 = 2*5.

Related

Matlab Matrixaddress [duplicate]

This question already has answers here:
How to obtain the mirror image of an array (MATLAB)?
(4 answers)
Closed 2 years ago.
During my course I came across that expression:
A(:,end:-1:1)
I have trouble to understand and read the morphemic structure of the 2nd Operand "end;-1;1"
Lets take the example:
A=[1 2 3; 4 5 6; 7 8 9]
I am aware of:
A(:).. Outputs [1 2 3; 4 5 6; 7 8 9] as rows. Operator is :.
A(1,:).. Outputs [1 2 3; 4 5 6; 7 8 9] as columns Operator is , then , .
A(:,1).. Outputs [1 2 3; 4 5 6; 7 8 9] as rows. Operator is , beforehand : .
A(:,end:-1:1)
Output in Matlab show me: 3x3 Matrix.
How am I supposed to read the structure?
Graphem: : ..show me the rows,
Graphem: end:-1 .. ??
Graphem: :1 ..
Somehow ":" was for me the operator for show all of the elements.
It makes sense to me that the "Operand1 , Operand2" shows me the 2 Dimensional Matrix.
First Idea:
The end:-1:1 expression seemed to me like a loop. So -1, 0, 1 => **3x Elements** ?
But when I type
A(1,end:3)
it only shows me the 3rd row.
Second Idea:
A(end:-1:1,1)
It shows me the inverted Matrix..
My background:
I am a undergraduate student from the field of language.
I build in my freetime the 8-Bit Sap1 according Ben Eater.
So I am familiar with program memory or instruction memory.
I understand only the result, but not how it is achieved by the MATLAB compiler.
Someone said to me that the "Matrixaddressing is somehow optimized".
Looking forward to a helpful answer in each step. :)
Thanks in advance!
The end keyword in matrix indexing indicates index of last element in the corresponding dimension. So, A(:,end:-1:1) simply means A(:, size(A, 2):-1:1), which in you example (A=[1 2 3; 4 5 6; 7 8 9]), is equivalent to A(:, 3:-1:1).
But to understand what it does, you need to know what 3:-1:1 does. It creates a subrange. You already know 1:3 creates [1, 2, 3]. 1:3 is simplified form of 1:1:3: rangeStrart:increment:rangeEnd. Now, 3:1 or 3:1:1 creates an empty vector, because rangeStart is greater than rangeEnd. To create [3, 2, 1] you need to use a negative step: 3:-1:1.
So, A(:,end:-1:1) means A(:, [3, 2, 1]), which inverts order of rows of A. Also, A(:,end:3) means A(:, 3:3) and eventually A(:, 3), which returns 3rd row of A.
Edit: about your misunderstandings, addressed by #CrisLuengo
I am aware of:
A(:).. Outputs [1 2 3; 4 5 6; 7 8 9] as rows. Operator is :.
A(1,:).. Outputs [1 2 3; 4 5 6; 7 8 9] as columns Operator is , then , .
A(:,1).. Outputs [1 2 3; 4 5 6; 7 8 9] as rows. Operator is ,beforehand : .
A(3, 2) is the element in the 3,2 position (third row, second column) of A
A(1, :) is equivalent to A(1, 1:size(A, 2)) and A(1, 1:end) and is the first row of A
A(:, 1) is equivalent to A(1:size(A, 1), 1) and A(1:end, 1) and is the first column of A
A(:) is equivalent to A(1:numel(A)) and is a single column containing all elements of A
In MATLAB, when accessing an array, it is accessed as A(row#,col#). row# and col# can either be integers or a vector of integers. If they are integers then one spot in the matrix is accessed. If they are vectors then MATLAB will loop through the vector and choose spots in A which correspond to the integers in the vector.
end:-1:1 creates a vector which contains the integers ranging from the number of columns (in this case because you put this vector in the col section: A(row#,col#)) to 1. Ex: 4x5 matrix, end:-1:1 would be [5 4 3 2 1].
When you put : in the row part of the matrix, that means you access all rows of the matrix.
Here's an example of A(:,end:-1:1)
The col# vector (:) is [1 2 3] and the row# vector (end:-1:1) is [3 2 1]
A = [1 2 3;
4 5 6;
7 8 9]
A(:,end:-1:1)
[3, 5, 7]
I think you're overthinking this slightly.
If we have a vector
A = [1 2 3]
and we call A(end:-1:1), then we get a vector [3 2 1]. The indexing has returned the same vector, with the values reversed. If we now have a matrix
A = [1 2 3; 4 5 6; 7 8 9]
and call A(:, end:-1:1), we get the matrix with the same values in each row, but now the columns have been reversed to give
A(:, end:-1:1) = [3 2 1; 6 5 4; 9 8 7].
Recall what the colon means in this context.
If we define a vector, v = (1:10), we get a vector with the first element being 1, the last element being 10 and each value in between being integers in steps of 1. If we instead define v = (1:2:10), we get the same, but the elements are separated by 2, not 1.
end:-1:1 is a vector made in just the same way. The first number is the final element in the row of A, and the final number is the first element in the row. Each number is separated by a value of -1. If we try
v = 10:-1:1
we get [10 9 8 7 6 5 4 3 2 1]. If we call v(2:4) we get the second, third and fourth elements of v. If we call v(1:end), we simply get v. If we call v(end:-1:1), we return v, with the elements in the reversed order.
Edit A typo.

find number distinct elements in a matrix matlab

say that I have a matrix A and an integer E = 16
A = [4 3 0.987477222842622;
8 4 0.987477222842622;
3 8 0.994241214094179;
12 5 0.951729278021845;
13 4 0.972221868548579;
14 13 0.996357467281480;
16 12 0.994198825847544]
It means that
element 4 is equal to element 3 with a similarity of 0.98,
element 8 is equal to element 4 with a similarity of 0.98,
element 3 is equal to element 8 with a similarity of 0.99 etc..
please note that if element 13 is equal to element 4 (5th row) it doesn't mean that it is also equal to element 3 (since 4 is equal to 3 ,see first row). The similarity between 13 and 3 apparently is less than the threshold (0.95) to be considered equal.
In total there are E=16 elements.
I would like to find the number of distinct elements...
In this case I have 16-5=11 different elements because the first 3 couples in the matrix (4-3,8-4,3-8) should be considered as a unique element...
That because
4-3 -> 1 element
8-4 -> 1 element
3-8 -> 1 element, but since 8 is equal to 4 that is equal to 3 they should considered as a unique element..
Do you have a solution for this problem?
it's driving me crazy :)

Find Starting index of the decreasing sequence

I have a sequence extracted from the measurement file and the sequence is shown below.
a=[2 1 3 2 1 0 1 2 3 4 5 4 3 2 3 4 5 4];
I want to find the starting indices of each decreasing sequence....
for eg: In the above sequence you can find the sequence starts decreasing at the following indices
1. [3 2 1] this sequence starts decreasing from the index 3,
2. [5 4 3 2] this sequence starts decreasing from the index 11,
3. [5 4] this sequence starts decreasing from the index 17.
Any idea regarding how to find this sequence starting point will be more useful... Thanks in advance
How about:
find(diff([0, diff(a) < 0]) == 1)
In other words find the index locations where the difference is negative (diff(a) < 0) and then choose only those that came after an increasing number.

Removing certain elements of an array based on another array (MATLAB)

I have arrays
A = [7 4 6 1 2 3 5]
B = [1 5 4 0 0 2 0]
(Array A will always have length=7 with the numbers in a random order.)
I want to keep the order of A, but only retain the values if its corresponding index of B is >0.
So from the example above, I'd want to change A to [6 1 2 3]. Meaning, the 7th, 4th, and 5th elements in B equal 0, so delete values 7, 4, and 5 from A.
I'd like to do this without a loop.
A(B(A)>0)
or:
A(find(B(A)))
though I believe the latter is less efficient

How to extract the N minimum points and N maximum points (ignoring repeating points in an array) in MATLAB?

I have a sorted array of points like
x=[1 1 1 2 2 4 4 5 6 ......7 8 8 9 9]
I want to have an array containing 3 elements with minimum elements and 3 with maximum elements of this array (ignoring the same elements)
The desired results for the above would be
ans=[1 2 4 7 8 9]
A bit less elegant, but takes advantage of sorted input, so much faster.
i = find(diff(x)~=0);
ans = x([i([1:3 end-1:end]) end]);
You can do this using the function UNIQUE:
uniqueValues = unique(x); %# Get the unique values of x
minmaxValues = uniqueValues([1:3 end-2:end]); %# Get the 3 smallest and largest