matrix-indexing

Can someone explain this example of deleting elements from a matrix in MATLAB?

旧城冷巷雨未停 提交于 2019-11-28 02:08:56
问题 The following example appears in the MATLAB tutorial: X = [16 2 13; 5 11 8; 9 7 12; 4 14 1] Using a single subscript deletes a single element, or sequence of elements, and reshapes the remaining elements into a row vector. So: X(2:2:10) = [] results in: X = [16 9 2 7 13 12 1] Mysteriously, the entire 2nd row and the first two elements in the 4th row have been deleted, but I can't see the correspondence between the position of the deleted elements and the index vector 2:2:10 . Can someone

How can I efficiently remove zeroes from a (non-sparse) matrix?

本小妞迷上赌 提交于 2019-11-27 16:19:03
问题 I have a matrix: x = [0 0 0 1 1 0 5 0 7 0]; I need to remove all of the zeroes, like so: x = [1 1 5 7]; The matrices I am using are large (1x15000) and I need to do this multiple times (5000+), so efficiency is key! 回答1: One way: x(x == 0) = []; A note on timing: As mentioned by woodchips, this method seems slow compared to the one used by KitsuneYMG. This has also been noted by Loren in one of her MathWorks blog posts. Since you mentioned having to do this thousands of times, you may notice

Indexing of unknown dimensional matrix

非 Y 不嫁゛ 提交于 2019-11-27 14:46:26
I have a non-fixed dimensional matrix M, from which I want to access a single element. The element's indices are contained in a vector J. So for example: M = rand(6,4,8,2); J = [5 2 7 1]; output = M(5,2,7,1) This time M has 4 dimensions, but this is not known in advance. This is dependent on the setup of the algorithm I'm writing. It could likewise be that M = rand(6,4); J = [3 1]; output = M(3,1) so I can't simply use output=M(J(1),J(2)) I was thinking of using sub2ind , but this also needs its variables comma separated.. @gnovice this works, but I intend to use this kind of element

In R, what does a negative index do?

孤者浪人 提交于 2019-11-27 09:28:13
I am porting part of a program (not enough to compile and run) from R to C++. I am not familiar with R. I have done okay using the references online, but was stumped by the following line: cnt2.2<-cnt2[,-1] I am guessing: cnt2 is a 2 dimensional matrix cnt2.2 is a new variable being declared with a period '.' used the same way an alphabetic character would be. <- is an assignment. [,-1] accesses part of the array. I thought [,5] meant all rows, 5th column only. If this is correct, I have no idea what -1 refers to. This is covered in section 2.7 of the manual: http://cran.r-project.org/doc

Numpy advanced selection not working

久未见 提交于 2019-11-27 07:29:24
问题 Can someone please help me to understand why sometimes the advanced selection doesn't work and what I can do to get it to work (2nd case)? >>> import numpy as np >>> b = np.random.rand(5, 14, 3, 2) # advanced selection works as expected >>> b[[0,1],[0,1]] array([[[ 0.7575555 , 0.18989068], [ 0.06816789, 0.95760398], [ 0.88358107, 0.19558106]], [[ 0.62122898, 0.95066355], [ 0.62947885, 0.00297711], [ 0.70292323, 0.2109297 ]]]) # doesn't work - why? >>> b[[0,1],[0,1,2]] Traceback (most recent

Matlab index to logic indexing

删除回忆录丶 提交于 2019-11-27 06:01:01
问题 I have given a list of indices, e.g. i = [3 5] and a vector v = 1:6 . I need a function f which returns the logical map for the vector v given the indices i , e.g.: f(i, length(v)) = [0 0 1 0 1 0] Since I will call this function several million times, I would like to make it as fast as possible. Is there a builtin function which performs this task? 回答1: I know I'm late in the game, but I really wanted to find a faster solution which is just as elegant as ismember . And indeed there is one,

How can I find the maximum value and its index in array in MATLAB?

别来无恙 提交于 2019-11-27 04:35:46
Suppose I have an array, a = [2 5 4 7] . What is the function returning the maximum value and its index? For example, in my case that function should return 7 as the maximum value and 4 as the index. Acorbe The function is max . To obtain the first maximum value you should do [val, idx] = max(a); val is the maximum value and idx is its index. For a matrix you can use this: [M,I] = max(A(:)) I is the index of A(:) containing the largest element. Now, use the ind2sub function to extract the row and column indices of A corresponding to the largest element. [I_row, I_col] = ind2sub(size(A),I)

How do I remove elements at a set of indices in a vector in MATLAB?

 ̄綄美尐妖づ 提交于 2019-11-27 03:44:59
问题 I have a vector with 100 elements. I have another vector with index positions of elements I want to remove from this vector. How do I do this? 回答1: vector(indecies) = [] example: >> a = 1:10; >> a([3,4,7]) = [] a = 1 2 5 6 8 9 10 来源: https://stackoverflow.com/questions/3789847/how-do-i-remove-elements-at-a-set-of-indices-in-a-vector-in-matlab

In R, what does a negative index do?

丶灬走出姿态 提交于 2019-11-26 14:36:22
问题 I am porting part of a program (not enough to compile and run) from R to C++. I am not familiar with R. I have done okay using the references online, but was stumped by the following line: cnt2.2<-cnt2[,-1] I am guessing: cnt2 is a 2 dimensional matrix cnt2.2 is a new variable being declared with a period '.' used the same way an alphabetic character would be. <- is an assignment. [,-1] accesses part of the array. I thought [,5] meant all rows, 5th column only. If this is correct, I have no

How can I find the maximum value and its index in array in MATLAB?

元气小坏坏 提交于 2019-11-26 12:28:06
问题 Suppose I have an array, a = [2 5 4 7] . What is the function returning the maximum value and its index? For example, in my case that function should return 7 as the maximum value and 4 as the index. 回答1: The function is max . To obtain the first maximum value you should do [val, idx] = max(a); val is the maximum value and idx is its index. 回答2: For a matrix you can use this: [M,I] = max(A(:)) I is the index of A(:) containing the largest element. Now, use the ind2sub function to extract the