matrix-indexing

Complement subset in Matlab [duplicate]

為{幸葍}努か 提交于 2020-01-21 23:01:18
问题 This question already has an answer here : How do I remove elements at a set of indices in a vector in MATLAB? (1 answer) Closed 6 years ago . In R, I can do the following: v <- 11:20 v[-(4:5)] and get 11 12 13 16 17 18 19 20 , thus all indices except the 4th and 5th. Is there an equivalent in Matlab's indexing logic? However I wrap my mind around it, I do not seem to get the correct search terms to google my own result for this fairly elementary question. Note: Of course I might use some of

Find local maxima with logical indexing in MATLAB

旧时模样 提交于 2020-01-14 05:12:05
问题 I have used logical indexing before in MATLAB vectors for conditions like X = X(X < 6); Now however I would like to find local extrema using the same idea, but with "local" conditions. I would be interested in something like X = X(X(i) > X(i-1) & X(i) > X(i + 1)); I know this wouldn't work in the first and last elements of the vector and that there are better ways to find local extrema. This question is different from this previous one (Getting FFT peaks from data) in that I'm not specially

Matlab Convert Vector to Binary Matrix [duplicate]

让人想犯罪 __ 提交于 2020-01-11 09:12:11
问题 This question already has answers here : Create a zero-filled 2D array with ones at positions indexed by a vector (4 answers) Closed 3 years ago . I have a vector v of size (m,1) whose elements are integers picked from 1:n. I want to create a matrix M of size (m,n) whose elements M(i,j) are 1 if v(i) = j, and are 0 otherwise. I do not want to use loops, and would like to implement this as a simple vector-matrix manipulation only. So I thought first, to create a matrix with repeated elements M

Why am I getting the error “Index exceeds matrix dimensions”?

隐身守侯 提交于 2020-01-09 11:43:00
问题 I am currently new to MATLAB. My code is below. I just have a question regarding why I keep getting the error "Index exceeds matrix dimensions" for the functions provided: a = [105 97 245 163 207 134 218 199 160 196 221 154 228 131 180 178 157 151 ... 175 201 183 153 174 154 190 76 101 142 149 200 186 174 199 115 193 167 ... 171 163 87 176 121 120 181 160 194 184 165 145 160 150 181 168 158 208 ... 133 135 172 171 237 170 180 167 176 158 156 229 158 148 150 118 143 141 ... 110 133 123 146 169

Can characters be used as indices?

此生再无相见时 提交于 2020-01-04 01:01:10
问题 Let's define, for example, x = 10:10:2000; As is well known, integer values can be used as indices: >> x(9) ans = 90 In Matlab, characters can often be used where a number would be expected, with Matlab doing the conversion automatically. For example, since the ASCII code of 'a' is 97 , >> 'a'+1 ans = 98 Can characters be also used as indices? Does Matlab convert them into integers? 回答1: They can be used... but careful if the index is a single colon! Let's define >> x = 10:10:2000; Indexing

Vector as column index in matrix

妖精的绣舞 提交于 2019-12-30 11:09:52
问题 Given a matrix A ( mxn ) and a vector B ( mx1 ) I want to create a vector C ( mx1 ) in which each row element is the row element of A from a column indexed by B . Is it possible to do this, without using loops? A = [1 2; 3 4; 5 6]; B = [2 1 1].'; Then I want: C = [2 3 5].'; 回答1: Convert the column subscripts of B to linear indices and then use them to reference elements in A : idx = sub2ind(size(A), (1:size(A, 1)).', B); C = A(idx); (for more information, read the part about linear indexing

Vector as column index in matrix

主宰稳场 提交于 2019-12-30 11:09:05
问题 Given a matrix A ( mxn ) and a vector B ( mx1 ) I want to create a vector C ( mx1 ) in which each row element is the row element of A from a column indexed by B . Is it possible to do this, without using loops? A = [1 2; 3 4; 5 6]; B = [2 1 1].'; Then I want: C = [2 3 5].'; 回答1: Convert the column subscripts of B to linear indices and then use them to reference elements in A : idx = sub2ind(size(A), (1:size(A, 1)).', B); C = A(idx); (for more information, read the part about linear indexing

Fast matrix indexing from vectors

◇◆丶佛笑我妖孽 提交于 2019-12-25 05:01:06
问题 I want to do a lot of matrix indexing of a high-D array, but the indices are split up. I came up with a few solutions: ### setup test <- array(0, c(3,3,3,3)) test[1,2,3,2] <- 1 system.time(for (i in 1:1000000) test[1,2,3,2] ) ### index split between two vectors idx1 <- c(1,2); idx2 <- c(3,2) ### things that work are slower system.time(for (i in 1:1000000) test[rbind(c(idx1, idx2))] ) system.time(for (i in 1:1000000) test[matrix(c(idx1, idx2), nrow=1)] ) system.time(for (i in 1:1000000) test[t

Fast matrix indexing from vectors

守給你的承諾、 提交于 2019-12-25 05:01:05
问题 I want to do a lot of matrix indexing of a high-D array, but the indices are split up. I came up with a few solutions: ### setup test <- array(0, c(3,3,3,3)) test[1,2,3,2] <- 1 system.time(for (i in 1:1000000) test[1,2,3,2] ) ### index split between two vectors idx1 <- c(1,2); idx2 <- c(3,2) ### things that work are slower system.time(for (i in 1:1000000) test[rbind(c(idx1, idx2))] ) system.time(for (i in 1:1000000) test[matrix(c(idx1, idx2), nrow=1)] ) system.time(for (i in 1:1000000) test[t

Numpy 3d array indexing

时光毁灭记忆、已成空白 提交于 2019-12-22 10:45:51
问题 I have a 3d numpy array ( n_samples x num_components x 2 ) in the example below n_samples = 5 and num_components = 7. I have another array ( indices ) which is the selected component for each sample which is of shape ( n_samples ,). I want to select from the data array given the indices so that the resulting array is n_samples x 2 . The code is below: import numpy as np np.random.seed(77) data=np.random.randint(low=0, high=10, size=(5, 7, 2)) indices = np.array([0, 1, 6, 4, 5]) #how can I