Lexicographical order of an array of integers - lexicographic

How to find lexicographical order of an array of N integers(i.e. 1,2,3,...,n) in its permutations in O(n) time?
Ex:- lexicographical order of {2,3,1,4} is 9 in its permutations

Related

multiple indexing and finding mode of cell array

I have an array of integers called indices and a cell array of strings called Categories. How do I find the most frequent string out of only the cells in the array indices?
You can use unique to find the mode of a cell array by finding the mode of the third output and use that to index into the first output of unique. To determine the mode of only the elements specified by indices, you'll want to grab just the subset of elements of Categories indicated by indices and pass those to unique.
[values, ~, inds] = unique(Categories(indices));
modeValue = values{mode(inds)};

Sort rows of a matrix in ascending order

I would like to sort a matrix in ascending order, however I do not want to affect the third column. For example, the sorted version of
A= [ 2 1 3;
5 4 1;
4 3 2]
Would be
B= [1 2 3;
4 5 1;
3 4 2]
Matlab provides quite a bit of inhouse help so using help FUNCTION/CLASS would have provided you with the below information. If you don't know the FUNCTION\CLASS name use lookfor TERM for a list of matches or alternately docsearch TERM.
Stock matlab provides both sort and sortrows. You'll be needing the latter.
sortrows(X,C)
Where C is a list of column indices to sort by whose sign may be positive corresponding for ascending order or negative for descending order.
In your example you'll want this :
sortrows(A',[1,2])'
The ' indicates to matlab that you need the matrix transposed, which basically swaps rows and columns before and after sortrows is called.
You can just sort the 1st two columns and update the matrix accordingly:
edit: updated dimension
A(:,1:2) = sort(A(:,1:2),2);

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.

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

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.

matlab cell array, count number of different elements

I have 2 cell arrays as below:
A = {'S' 'M' 'N' 'E'};
B = {'E' 'M' 'Q' 'S'};
In this case, the number of different elements is 3.
In a number array, I can use length(find(A ~= B)); to easily count number of different elements in one step easily.
Is there something similar for cell array of characters?
EDIT: I think I've misunderstood your question, and you probably meant finding different elements in corresponding positions in the arrays. I still kept my old answer
Counting different elements at the same position
yuk's approach with strcmp is correct. However, it works only if the two arrays are of the same size. The generalized solution would be:
N = min(numel(A), numel(B));
sum(~strcmp(A(1:N), B(1:N))) + numel(A) + numel(B) - 2 * N
If the arrays are of different length, the "extra" elements in the larger array will be counted as different here.
Counting different elements in any position
The most general approach would be using ismember, which does not care about lengths of strings or their position in the array. To count the total number of elements in A and B that are different, just do:
sum(ismember(A, B)) + sum(ismember(B, A))
The same effect can also be obtained with setdiff (instead of ismember):
numel(setdiff(A, B)) + numel(setdiff(B, A))
Both ways are valid for any two arrays, not necessarily of equal size.
Try
cell2mat(A)==cell2mat(B)
to start with, the rest should be straightforward. This simple approach will fail if the cell arrays don't have the same dimensions.
If your cell array is a cell array of strings you can use STRCMP:
sum(~strcmp(A,B))
Of course make sure A and B have the same length.
By the way for numeric array it's more efficient to use sum(A~=B). In general find is slow.
U can also try unique([A B]) if A and B are given in the exemple you gave.
It A and B do not have the same dimension you can try this.
unique(reshape(cell2mat(A,1,[])),reshape(cell2mat(B,1,[])))