find k such that both part of array is sorted in increasing order - find

suppose we are given sorted array of n distinct numbers that has been rotated k steps, for some unknown integer k between 1 to n-1. "Rotated means array A is such that the first part of array A[1..K]" is sorted in increasing order, the suffix A[K+1...n] is also sorted in increasing order, but A[n] < A[1]. The problem is to find k.

Related

How do I save the indices of the top 4 maximum numbers in a matrix in scilab

I need to save the indices of 4 maximum numbers
for example, I need to get the indices of rows 10,9,7,5
5.0259327
4.7127487
4.8435524
4.8538644
5.1048996
6.2441973
5.9413803
6.2912638
5.1117512
5.8309519
5.7419509
6.9663477
5.9958319
6.9519781
6.5802736
6.7327558
7.6765878
I have used
[mA,nA]=max(distA)
where mA is the row and nA is the column
in getting one maximum number but I cannot figure out how to choose another maximum number without duplication. I cannot sort because I need the indices.
You can use the gsort function:
[S,ind]=gsort(distA,"g","d");
The index of the 4 largest elements is the given by
ind(1:4)
You can use this little trick.
[output_val, output_index] = max(input_mat(input_mat < max(input_mat)))
This will give you the value and index of the second largest element. And then similarly, you can do it for 4 numbers.

Accessing the layers of a multidimensional array and performing some function on each layers

I have this code
A = unidrnd(2,100,30)-1;
B = reshape(A, 100, 3, 10);
B is a multidimensional array with 10 layers of 100x3 Matrices. Now I want to perform this code,
C = length(nonzeros(all(B,2)))/100;
where the function on the right hand side of the code is suppose to generate 10 values corresponding to the result of the 10 layers, but all I get is a single value. The right hand of the code checks how many rows are all 1's. It takes the number of rows that are all 1's and divides it by 100 to obtain the fraction of the number of rows that are all 1's.
How can I obtain the result of every 100 x 3 layers of the 3D matrix using the single line of code I have shown above such that I do not have to use a loop? The result C had to be array of the results as expected.
You started out well. all(B,2) is good, it gives you the 100x1x10 matrix that's 1 where the corresponding rows are all 1's and 0 otherwise.
nonzeros, however, simply lists all of the nonzero elements of the entire matrix, in your case, a string of 1's, completely disregarding the dimensions of the array. You'd get the same results with nonzeros(A(:)) as with nonzeros(A).
[Note: nnz(A) would get you the same results as length(nonzeros(A)), but that's not what we want to do anyway.]
Since your matrix is binary (the output of all is a logical array), we can count the number of non-zero elements by summing the matrix elements. And sum gives us a dimension argument just like all, so we just sum the columns that all gave us.
C = sum(all(B,2),1)/100;
This gives you a 1x1x10 array of percentages. If you wanted that to just be a normal vector, you could use squeeze.
C = squeeze(sum(all(B,2),1)/100);

Count the number of similar values with same indices in two arrays MATLAB

I am trying to count the number of similar elements in two arrays, which can be done by the intersect function but I need to get only the values which are similar and have the same index. Any ideas?
If you are looking for how many entries in the two matrices are "nearly" the same, then set some tolerance tol, and then you want to find how many corresponding entries in your matrices (call them A and B) differ by less than tol.
abs(A-B)<tol
is a matrix the same size as A and B which has a 1 where the elements are close together, and a 0 where they're not. You can use
[i,j]=find(abs(A-B)<tol)
to get the positions of the nearly-mathcing elements, or
nnz(abs(A-B)<tol)
to just count how many values nearly-match.

Retrieve a specific permutation without storing all possible permutations in Matlab

I am working on 2D rectangular packing. In order to minimize the length of the infinite sheet (Width is constant) by changing the order in which parts are placed. For example, we could place 11 parts in 11! ways.
I could label those parts and save all possible permutations using perms function and run it one by one, but I need a large amount of memory even for 11 parts. I'd like to be able to do it for around 1000 parts.
Luckily, I don't need every possible sequence. I would like to index each permutation to a number. Test a random sequence and then use GA to converge the results to find the optimal sequence.
Therefore, I need a function which gives a specific permutation value when run for any number of times unlike randperm function.
For example, function(5,6) should always return say [1 4 3 2 5 6] for 6 parts. I don't need the sequences in a specific order, but the function should give the same sequence for same index. and also for some other index, the sequence should not be same as this one.
So far, I have used randperm function to generate random sequence for around 2000 iterations and finding a best sequence out of it by comparing length, but this works only for few number of parts. Also using randperm may result in repeated sequence instead of unique sequence.
Here's a picture of what I have done.
I can't save the outputs of randperm because I won't have a searchable function space. I don't want to find the length of the sheet for all sequences. I only need do it for certain sequence identified by certain index determined by genetic algorithm. If I use randperm, I won't have the sequence for all indexes (even though I only need some of them).
For example, take some function, 'y = f(x)', in the range [0,10] say. For each value of x, I get a y. Here y is my sheet length. x is the index of permutation. For any x, I find its sequence (the specific permutation) and then its corresponding sheet length. Based on the results of some random values of x, GA will generate me a new list of x to find a more optimal y.
I need a function that duplicates perms, (I guess perms are following the same order of permutations each time it is run because perms(1:4) will yield same results when run any number of times) without actually storing the values.
Is there a way to write the function? If not, then how do i solve my problem?
Edit (how i approached the problem):
In Genetic Algorithm, you need to crossover parents(permutations), But if you crossover permutations, you will get the numbers repeated. for eg:- crossing over 1 2 3 4 with 3 2 1 4 may result something like 3 2 3 4. Therefore, to avoid repetition, i thought of indexing each parent to a number and then convert the number to binary form and then crossover the binary indices to get a new binary number then convert it back to decimal and find its specific permutation. But then later on, i discovered i could just use ordered crossover of the permutations itself instead of crossing over their indices.
More details on Ordered Crossover could be found here
Below are two functions that together will generate permutations in lexographical order and return the nth permutation
For example, I can call
nth_permutation(5, [1 2 3 4])
And the output will be [1 4 2 3]
Intuitively, how long this method takes is linear in n. The size of the set doesn't matter. I benchmarked nth_permutations(n, 1:1000) averaged over 100 iterations and got the following graph
So timewise it seems okay.
function [permutation] = nth_permutation(n, set)
%%NTH_PERMUTATION Generates n permutations of set in lexographical order and
%%outputs the last one
%% set is a 1 by m matrix
set = sort(set);
permutation = set; %First permutation
for ii=2:n
permutation = next_permute(permutation);
end
end
function [p] = next_permute(p)
%Following algorithm from https://en.wikipedia.org/wiki/Permutation#Generation_in_lexicographic_order
%Find the largest index k such that p[k] < p[k+1]
larger = p(1:end-1) < p(2:end);
k = max(find(larger));
%If no such index exists, the permutation is the last permutation.
if isempty(k)
display('Last permutation reached');
return
end
%Find the largest index l greater than k such that p[k] < p[l].
larger = [false(1, k) p(k+1:end) > p(k)];
l = max(find(larger));
%Swap the value of p[k] with that of p[l].
p([k, l]) = p([l, k]);
%Reverse the sequence from p[k + 1] up to and including the final element p[n].
p(k+1:end) = p(end:-1:k+1);
end

Matlab - show corresponding values of 1st column of matrix, baring in mind the last 2 values of the 2nd column

I created a matrix with two columns, using two vectors, f and PS, that I already had:
M = [f PS]; %81x2 matrix
And then I sorted the matrix with respect to "PS" (second column), in order to have the maximum values of "PS" at the last positions of the vector:
M1=sortrows(M,2); %81x2 matrix
And I got something like this:
f...PS
5...83
10...136
3...357
1...985
Since now I assured the last two values of "PS" are indeed the maximum values, the information I want to give to the user is 1 and 3 (f values corresponding to the 2 maximum values of PS, which are at the bottom).
How can I do this?
You can use the end index: M1(end,1) and M1(end-1,1) should contain 1 and 3, respectively.
Best,