matlab,matrix,row,matrix-indexing

One approach to select those valid rows from x - x_valid = x(sum(x(:,[2 3 5]),2)>=2,:) Instead, if you are interested in knowing just the valid indices - idx = find(sum(x(:,[2 3 5]),2)>=2) Sample run - x = 0 1 1 0 1 1 1 1 0 0 0 0 1...

arrays,matlab,multidimensional-array,matrix-indexing

One approach with logical indexing - mask = A>999; %// get the 3D mask mask(:,:,1) = 0; %// set all elements in the first 3D slice to zeros, %// to neglect their effect when we mask the entire input array with it A(mask) = Inf %// finally mask and set...

You can use use, for example: I = np.array([[1,2,3], [2,1,3], [4,1,6]]) M = np.ndarray((10,10,10,10)) X = np.array([M[t,:] for t in I]) ...

This works by building a mask (logical index) with the desired diamond shape and a specified center. The mask is obtained by computing the L1 (or taxicab) distance from each entry to the diamond center and comparing to the appropriate threshold: A = rand(7,9); %// example matrix pos_row = 3;...

matlab,matrix,repetition,matrix-indexing

Approach #1 diff and sort based approach that must be pretty efficient - sortedmatrix = sort(matrix,2) result = matrix(all(diff(sortedmatrix,[],2)~=0,2),:) Breaking it down to few steps for explanation Sort along the columns, so that the duplicate values in each row end up next to each other. We used sort for this...

Indexing a small vector using a large matrix is a (nice) way of performing a look-up-table operation: that is output is generated by replacing each element of A by the element B(A+1) the result is the same size as A. In your particular example, since B( A(ii,jj)+1 ) == A(ii,jj)...

You can use this function: insert.diag <- function(A,b,start=c(1,1),dir=c(1,1)) { sq <- seq_along(b)-1 indices <- sapply(1:2,function(i) start[i] + dir[i]*sq) stopifnot(all(indices>0)) stopifnot(all(indices[,1]<=nrow(A))) stopifnot(all(indices[,2]<=ncol(A))) A[indices] <- b A } Some examples of use: A <- matrix(nrow=6,ncol=6) b <- c(1:4) > insert.diag(A,b,c(1,6),c(1,-1)) [,1] [,2] [,3] [,4] [,5] [,6] [1,] NA NA NA NA NA...

Assuming M to be the input array, you can use the pattern of decaying offsets [10 9 8 7 6 5 ...] between the consecutive row IDs of A alongwith cumsum to get the actual row IDs in M. Then, use matrix-indexing to get the corresponding rows of M for...

matlab,matrix,find,matrix-indexing

To retrieve the original indices, you have to keep the memory of the indices of your two conditions on x and y (which I put in the arrays cX and cY) and then use the function ind2sub. NB: your code is a little bit confusing since x stands for the...

From the advanced indexing documentation - bi rico's link. Example Suppose x.shape is (10,20,30) and ind is a (2,3,4)-shaped indexing intp array, thenresult = x[...,ind,:] has shape (10,2,3,4,30) because the (20,)-shaped subspace has been replaced with a (2,3,4)-shaped broadcasted indexing subspace. If we let i, j, kloop over the (2,3,4)-shaped subspace then result[...,i,j,k,:] =x[...,ind[i,j,k],:]. This example produces the same result as x.take(ind, axis=-2). ...

arrays,julia-lang,matrix-indexing

Yes, arrays are indexed from 1

You cannot do this directly as you described. You will have to create additional data which may itself be tested logically, the output of which may be used to index into your vector. In your case, the best method would be to calculate an approximate derivative, and find downward zero-crossings...

So, I feel that there should be a way to do this using tree-based algorithm or a disjoint-set/union-find type approach. But for a dirty first approach, which is likely not all that efficient, I coded the following: A = [5 4 8 6 9 8 10 9 15 14 16...

Now that you've explained what you want, transpose the matrices, select only the three top rows of A, and then use the third argument of ismember to specify that you want to compare entire rows: A(:,ismember(A(1:3,:).', B.', 'rows').') 20 4 4 20 20 85 85 85 36 1 1 36...